home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / gnu / a2_0b_Emacs_sr.lha / Emacs-19.25 / src / fileio.c! < prev    next >
Text File  |  1995-01-03  |  120KB  |  4,284 lines

  1. /* File IO for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <config.h>
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24.  
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28.  
  29. #if !defined (S_ISLNK) && defined (S_IFLNK)
  30. #  define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
  31. #endif
  32.  
  33. #if !defined (S_ISREG) && defined (S_IFREG)
  34. #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  35. #endif
  36.  
  37. #ifdef VMS
  38. #include "vms-pwd.h"
  39. #else
  40. #include <pwd.h>
  41. #endif
  42.  
  43. #ifdef MSDOS
  44. #include "msdos.h"
  45. #include <sys/param.h>
  46. #endif
  47.  
  48. #include <ctype.h>
  49.  
  50. #ifdef VMS
  51. #include "vmsdir.h"
  52. #include <perror.h>
  53. #include <stddef.h>
  54. #include <string.h>
  55. #endif
  56.  
  57. #include <errno.h>
  58.  
  59. #ifndef vax11c
  60. extern int errno;
  61. #endif
  62.  
  63. extern char *strerror ();
  64.  
  65. #ifdef APOLLO
  66. #include <sys/time.h>
  67. #endif
  68.  
  69. #ifndef USG
  70. #ifndef VMS
  71. #ifndef BSD4_1
  72. #define HAVE_FSYNC
  73. #endif
  74. #endif
  75. #endif
  76.  
  77. #include "lisp.h"
  78. #include "intervals.h"
  79. #include "buffer.h"
  80. #include "window.h"
  81.  
  82. #ifdef VMS
  83. #include <file.h>
  84. #include <rmsdef.h>
  85. #include <fab.h>
  86. #include <nam.h>
  87. #endif
  88.  
  89. #include "systime.h"
  90.  
  91. #ifdef HPUX
  92. #include <netio.h>
  93. #ifndef HPUX8
  94. #ifndef HPUX9
  95. #include <errnet.h>
  96. #endif
  97. #endif
  98. #endif
  99.  
  100. #ifndef O_WRONLY
  101. #define O_WRONLY 1
  102. #endif
  103.  
  104. #define min(a, b) ((a) < (b) ? (a) : (b))
  105. #define max(a, b) ((a) > (b) ? (a) : (b))
  106.  
  107. /* Nonzero during writing of auto-save files */
  108. int auto_saving;
  109.  
  110. /* Set by auto_save_1 to mode of original file so Fwrite_region will create
  111.    a new file with the same mode as the original */
  112. int auto_save_mode_bits;
  113.  
  114. /* Alist of elements (REGEXP . HANDLER) for file names 
  115.    whose I/O is done with a special handler.  */
  116. Lisp_Object Vfile_name_handler_alist;
  117.  
  118. /* Functions to be called to process text properties in inserted file.  */
  119. Lisp_Object Vafter_insert_file_functions;
  120.  
  121. /* Functions to be called to create text property annotations for file.  */
  122. Lisp_Object Vwrite_region_annotate_functions;
  123.  
  124. /* File name in which we write a list of all our auto save files.  */
  125. Lisp_Object Vauto_save_list_file_name;
  126.  
  127. /* Nonzero means, when reading a filename in the minibuffer,
  128.  start out by inserting the default directory into the minibuffer. */
  129. int insert_default_directory;
  130.  
  131. /* On VMS, nonzero means write new files with record format stmlf.
  132.    Zero means use var format.  */
  133. int vms_stmlf_recfm;
  134.  
  135. /* These variables describe handlers that have "already" had a chance
  136.    to handle the current operation.
  137.  
  138.    Vinhibit_file_name_handlers is a list of file name handlers.
  139.    Vinhibit_file_name_operation is the operation being handled.
  140.    If we try to handle that operation, we ignore those handlers.  */
  141.  
  142. static Lisp_Object Vinhibit_file_name_handlers;
  143. static Lisp_Object Vinhibit_file_name_operation;
  144.  
  145. Lisp_Object Qfile_error, Qfile_already_exists;
  146.  
  147. Lisp_Object Qfile_name_history;
  148.  
  149. Lisp_Object Qcar_less_than_car;
  150.  
  151. report_file_error (string, data)
  152.      char *string;
  153.      Lisp_Object data;
  154. {
  155.   Lisp_Object errstring;
  156.  
  157.   errstring = build_string (strerror (errno));
  158.  
  159.   /* System error messages are capitalized.  Downcase the initial
  160.      unless it is followed by a slash.  */
  161.   if (XSTRING (errstring)->data[1] != '/')
  162.     XSTRING (errstring)->data[0] = DOWNCASE (XSTRING (errstring)->data[0]);
  163.  
  164.   while (1)
  165.     Fsignal (Qfile_error,
  166.          Fcons (build_string (string), Fcons (errstring, data)));
  167. }
  168.  
  169. close_file_unwind (fd)
  170.      Lisp_Object fd;
  171. {
  172.   close (XFASTINT (fd));
  173. }
  174.  
  175. /* Restore point, having saved it as a marker.  */
  176.  
  177. restore_point_unwind (location)
  178.      Lisp_Object location; 
  179. {
  180.   SET_PT (marker_position (location));
  181.   Fset_marker (location, Qnil, Qnil);
  182. }
  183.  
  184. Lisp_Object Qexpand_file_name;
  185. Lisp_Object Qdirectory_file_name;
  186. Lisp_Object Qfile_name_directory;
  187. Lisp_Object Qfile_name_nondirectory;
  188. Lisp_Object Qunhandled_file_name_directory;
  189. Lisp_Object Qfile_name_as_directory;
  190. Lisp_Object Qcopy_file;
  191. Lisp_Object Qmake_directory;
  192. Lisp_Object Qdelete_directory;
  193. Lisp_Object Qdelete_file;
  194. Lisp_Object Qrename_file;
  195. Lisp_Object Qadd_name_to_file;
  196. Lisp_Object Qmake_symbolic_link;
  197. Lisp_Object Qfile_exists_p;
  198. Lisp_Object Qfile_executable_p;
  199. Lisp_Object Qfile_readable_p;
  200. Lisp_Object Qfile_symlink_p;
  201. Lisp_Object Qfile_writable_p;
  202. Lisp_Object Qfile_directory_p;
  203. Lisp_Object Qfile_accessible_directory_p;
  204. Lisp_Object Qfile_modes;
  205. Lisp_Object Qset_file_modes;
  206. Lisp_Object Qfile_newer_than_file_p;
  207. Lisp_Object Qinsert_file_contents;
  208. Lisp_Object Qwrite_region;
  209. Lisp_Object Qverify_visited_file_modtime;
  210. Lisp_Object Qset_visited_file_modtime;
  211.  
  212. DEFUN ("find-file-name-handler", Ffind_file_name_handler, Sfind_file_name_handler, 2, 2, 0,
  213.   "Return FILENAME's handler function for OPERATION, if it has one.\n\
  214. Otherwise, return nil.\n\
  215. A file name is handled if one of the regular expressions in\n\
  216. `file-name-handler-alist' matches it.\n\n\
  217. If OPERATION equals `inhibit-file-name-operation', then we ignore\n\
  218. any handlers that are members of `inhibit-file-name-handlers',\n\
  219. but we still do run any other handlers.  This lets handlers\n\
  220. use the standard functions without calling themselves recursively.")
  221.   (filename, operation)
  222.     Lisp_Object filename, operation;
  223. {
  224.   /* This function must not munge the match data.  */
  225.   Lisp_Object chain, inhibited_handlers;
  226.  
  227.   CHECK_STRING (filename, 0);
  228.  
  229.   if (EQ (operation, Vinhibit_file_name_operation))
  230.     inhibited_handlers = Vinhibit_file_name_handlers;
  231.   else
  232.     inhibited_handlers = Qnil;
  233.  
  234.   for (chain = Vfile_name_handler_alist; XTYPE (chain) == Lisp_Cons;
  235.        chain = XCONS (chain)->cdr)
  236.     {
  237.       Lisp_Object elt;
  238.       elt = XCONS (chain)->car;
  239.       if (XTYPE (elt) == Lisp_Cons)
  240.     {
  241.       Lisp_Object string;
  242.       string = XCONS (elt)->car;
  243.       if (XTYPE (string) == Lisp_String
  244.           && fast_string_match (string, filename) >= 0)
  245.         {
  246.           Lisp_Object handler, tem;
  247.  
  248.           handler = XCONS (elt)->cdr;
  249.           tem = Fmemq (handler, inhibited_handlers);
  250.           if (NILP (tem))
  251.         return handler;
  252.         }
  253.     }
  254.  
  255.       QUIT;
  256.     }
  257.   return Qnil;
  258. }
  259.  
  260. DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory,
  261.   1, 1, 0,
  262.   "Return the directory component in file name NAME.\n\
  263. Return nil if NAME does not include a directory.\n\
  264. Otherwise return a directory spec.\n\
  265. Given a Unix syntax file name, returns a string ending in slash;\n\
  266. on VMS, perhaps instead a string ending in `:', `]' or `>'.")
  267.   (file)
  268.      Lisp_Object file;
  269. {
  270.   register unsigned char *beg;
  271.   register unsigned char *p;
  272.   Lisp_Object handler;
  273.  
  274.   CHECK_STRING (file, 0);
  275.  
  276.   /* If the file name has special constructs in it,
  277.      call the corresponding file handler.  */
  278.   handler = Ffind_file_name_handler (file, Qfile_name_directory);
  279.   if (!NILP (handler))
  280.     return call2 (handler, Qfile_name_directory, file);
  281.  
  282. #ifdef FILE_SYSTEM_CASE
  283.   file = FILE_SYSTEM_CASE (file);
  284. #endif
  285.   beg = XSTRING (file)->data;
  286.   p = beg + XSTRING (file)->size;
  287.  
  288.   while (p != beg && p[-1] != '/'
  289. #ifdef    AMIGA
  290.           && p[-1] != ':' /* CHFIXME: replace this call with AmigaDOS function? */
  291. #endif    /* AMIGA */
  292. #ifdef VMS
  293.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  294. #endif /* VMS */
  295. #ifdef MSDOS
  296.      && p[-1] != ':'
  297. #endif
  298.      ) p--;
  299.  
  300.   if (p == beg)
  301.     return Qnil;
  302. #ifdef MSDOS
  303.   /* Expansion of "c:" to drive and default directory.  */
  304.   if (p == beg + 2 && beg[1] == ':')
  305.     {
  306.       int drive = (*beg) - 'a';
  307.       /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir.  */
  308.       unsigned char *res = alloca (MAXPATHLEN + 5);
  309.       if (getdefdir (drive + 1, res + 2)) 
  310.     {
  311.       res[0] = drive + 'a';
  312.       res[1] = ':';
  313.       if (res[strlen (res) - 1] != '/')
  314.         strcat (res, "/");
  315.       beg = res;
  316.       p = beg + strlen (beg);
  317.     }
  318.     }
  319. #endif
  320.   return make_string (beg, p - beg);
  321. }
  322.  
  323. DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, Sfile_name_nondirectory,
  324.   1, 1, 0,
  325.   "Return file name NAME sans its directory.\n\
  326. For example, in a Unix-syntax file name,\n\
  327. this is everything after the last slash,\n\
  328. or the entire name if it contains no slash.")
  329.   (file)
  330.      Lisp_Object file;
  331. {
  332.   register unsigned char *beg, *p, *end;
  333.   Lisp_Object handler;
  334.  
  335.   CHECK_STRING (file, 0);
  336.  
  337.   /* If the file name has special constructs in it,
  338.      call the corresponding file handler.  */
  339.   handler = Ffind_file_name_handler (file, Qfile_name_nondirectory);
  340.   if (!NILP (handler))
  341.     return call2 (handler, Qfile_name_nondirectory, file);
  342.  
  343.   beg = XSTRING (file)->data;
  344.   end = p = beg + XSTRING (file)->size;
  345.  
  346.   while (p != beg && p[-1] != '/'
  347. #ifdef    AMIGA
  348.          && p[-1] != ':' /* CHFIXME: dos.library call, also check this function and above */
  349. #endif    /* AMIGA */
  350. #ifdef VMS
  351.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  352. #endif /* VMS */
  353. #ifdef MSDOS
  354.      && p[-1] != ':'
  355. #endif
  356.      ) p--;
  357.  
  358.   return make_string (p, end - p);
  359. }
  360.  
  361. DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, Sunhandled_file_name_directory, 1, 1, 0,
  362.   "Return a directly usable directory name somehow associated with FILENAME.\n\
  363. A `directly usable' directory name is one that may be used without the\n\
  364. intervention of any file handler.\n\
  365. If FILENAME is a directly usable file itself, return\n\
  366. (file-name-directory FILENAME).\n\
  367. The `call-process' and `start-process' functions use this function to\n\
  368. get a current directory to run processes in.")
  369.   (filename)
  370.     Lisp_Object filename;
  371. {
  372.   Lisp_Object handler;
  373.  
  374.   /* If the file name has special constructs in it,
  375.      call the corresponding file handler.  */
  376.   handler = Ffind_file_name_handler (filename, Qunhandled_file_name_directory);
  377.   if (!NILP (handler))
  378.     return call2 (handler, Qunhandled_file_name_directory, filename);
  379.  
  380.   return Ffile_name_directory (filename);
  381. }
  382.  
  383.  
  384. char *
  385. file_name_as_directory (out, in)
  386.      char *out, *in;
  387. {
  388.   int size = strlen (in) - 1;
  389.  
  390.   strcpy (out, in);
  391.  
  392. #ifdef VMS
  393.   /* Is it already a directory string? */
  394.   if (in[size] == ':' || in[size] == ']' || in[size] == '>')
  395.     return out;
  396.   /* Is it a VMS directory file name?  If so, hack VMS syntax.  */
  397.   else if (! index (in, '/')
  398.        && ((size > 3 && ! strcmp (&in[size - 3], ".DIR"))
  399.            || (size > 3 && ! strcmp (&in[size - 3], ".dir"))
  400.            || (size > 5 && (! strncmp (&in[size - 5], ".DIR", 4)
  401.                 || ! strncmp (&in[size - 5], ".dir", 4))
  402.            && (in[size - 1] == '.' || in[size - 1] == ';')
  403.            && in[size] == '1')))
  404.     {
  405.       register char *p, *dot;
  406.       char brack;
  407.  
  408.       /* x.dir -> [.x]
  409.      dir:x.dir --> dir:[x]
  410.      dir:[x]y.dir --> dir:[x.y] */
  411.       p = in + size;
  412.       while (p != in && *p != ':' && *p != '>' && *p != ']') p--;
  413.       if (p != in)
  414.     {
  415.       strncpy (out, in, p - in);
  416.       out[p - in] = '\0';
  417.       if (*p == ':')
  418.         {
  419.           brack = ']';
  420.           strcat (out, ":[");
  421.         }
  422.       else
  423.         {
  424.           brack = *p;
  425.           strcat (out, ".");
  426.         }
  427.       p++;
  428.     }
  429.       else
  430.     {
  431.       brack = ']';
  432.       strcpy (out, "[.");
  433.     }
  434.       dot = index (p, '.');
  435.       if (dot)
  436.     {
  437.       /* blindly remove any extension */
  438.       size = strlen (out) + (dot - p);
  439.       strncat (out, p, dot - p);
  440.     }
  441.       else
  442.     {
  443.       strcat (out, p);
  444.       size = strlen (out);
  445.     }
  446.       out[size++] = brack;
  447.       out[size] = '\0';
  448.     }
  449. #else /* not VMS */
  450. #ifdef    AMIGA /* CHFIXME: check */
  451.   /* AmigaDOS syntax, append slash if the last char isn't a ':' or '/' */
  452.   if (out[size] != '/' && out[size] != ':' && size != 0)
  453.     strcat (out, "/");
  454. #else    /* not AMIGA */
  455.   /* For Unix syntax, Append a slash if necessary */
  456. #ifdef MSDOS
  457.   if (out[size] != ':' && out[size] != '/')
  458. #else
  459.   if (out[size] != '/')
  460. #endif
  461.     strcat (out, "/");
  462. #endif /* not AMIGA */
  463. #endif /* not VMS */
  464.   return out;
  465. }
  466.  
  467. DEFUN ("file-name-as-directory", Ffile_name_as_directory,
  468.        Sfile_name_as_directory, 1, 1, 0,
  469.   "Return a string representing file FILENAME interpreted as a directory.\n\
  470. This operation exists because a directory is also a file, but its name as\n\
  471. a directory is different from its name as a file.\n\
  472. The result can be used as the value of `default-directory'\n\
  473. or passed as second argument to `expand-file-name'.\n\
  474. For a Unix-syntax file name, just appends a slash.\n\
  475. On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc.")
  476.   (file)
  477.      Lisp_Object file;
  478. {
  479.   char *buf;
  480.   Lisp_Object handler;
  481.  
  482.   CHECK_STRING (file, 0);
  483.   if (NILP (file))
  484.     return Qnil;
  485.  
  486.   /* If the file name has special constructs in it,
  487.      call the corresponding file handler.  */
  488.   handler = Ffind_file_name_handler (file, Qfile_name_as_directory);
  489.   if (!NILP (handler))
  490.     return call2 (handler, Qfile_name_as_directory, file);
  491.  
  492.   buf = (char *) alloca (XSTRING (file)->size + 10);
  493.   return build_string (file_name_as_directory (buf, XSTRING (file)->data));
  494. }
  495.  
  496. /*
  497.  * Convert from directory name to filename.
  498.  * On VMS:
  499.  *       xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
  500.  *       xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
  501.  * On UNIX, it's simple: just make sure there is a terminating /
  502.  
  503.  * Value is nonzero if the string output is different from the input.
  504.  */
  505.  
  506. directory_file_name (src, dst)
  507.      char *src, *dst;
  508. {
  509.   long slen;
  510. #ifdef VMS
  511.   long rlen;
  512.   char * ptr, * rptr;
  513.   char bracket;
  514.   struct FAB fab = cc$rms_fab;
  515.   struct NAM nam = cc$rms_nam;
  516.   char esa[NAM$C_MAXRSS];
  517. #endif /* VMS */
  518.  
  519.   slen = strlen (src);
  520. #ifdef VMS
  521.   if (! index (src, '/')
  522.       && (src[slen - 1] == ']'
  523.       || src[slen - 1] == ':'
  524.       || src[slen - 1] == '>'))
  525.     {
  526.       /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */
  527.       fab.fab$l_fna = src;
  528.       fab.fab$b_fns = slen;
  529.       fab.fab$l_nam = &nam;
  530.       fab.fab$l_fop = FAB$M_NAM;
  531.  
  532.       nam.nam$l_esa = esa;
  533.       nam.nam$b_ess = sizeof esa;
  534.       nam.nam$b_nop |= NAM$M_SYNCHK;
  535.  
  536.       /* We call SYS$PARSE to handle such things as [--] for us. */
  537.       if (SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL)
  538.     {
  539.       slen = nam.nam$b_esl;
  540.       if (esa[slen - 1] == ';' && esa[slen - 2] == '.')
  541.         slen -= 2;
  542.       esa[slen] = '\0';
  543.       src = esa;
  544.     }
  545.       if (src[slen - 1] != ']' && src[slen - 1] != '>')
  546.     {
  547.       /* what about when we have logical_name:???? */
  548.       if (src[slen - 1] == ':')
  549.         {            /* Xlate logical name and see what we get */
  550.           ptr = strcpy (dst, src); /* upper case for getenv */
  551.           while (*ptr)
  552.         {
  553.           if ('a' <= *ptr && *ptr <= 'z')
  554.             *ptr -= 040;
  555.           ptr++;
  556.         }
  557.           dst[slen - 1] = 0;    /* remove colon */
  558.           if (!(src = egetenv (dst)))
  559.         return 0;
  560.           /* should we jump to the beginning of this procedure?
  561.          Good points: allows us to use logical names that xlate
  562.          to Unix names,
  563.          Bad points: can be a problem if we just translated to a device
  564.          name...
  565.          For now, I'll punt and always expect VMS names, and hope for
  566.          the best! */
  567.           slen = strlen (src);
  568.           if (src[slen - 1] != ']' && src[slen - 1] != '>')
  569.         { /* no recursion here! */
  570.           strcpy (dst, src);
  571.           return 0;
  572.         }
  573.         }
  574.       else
  575.         {        /* not a directory spec */
  576.           strcpy (dst, src);
  577.           return 0;
  578.         }
  579.     }
  580.       bracket = src[slen - 1];
  581.  
  582.       /* If bracket is ']' or '>', bracket - 2 is the corresponding
  583.      opening bracket.  */
  584.       ptr = index (src, bracket - 2);
  585.       if (ptr == 0)
  586.     { /* no opening bracket */
  587.       strcpy (dst, src);
  588.       return 0;
  589.     }
  590.       if (!(rptr = rindex (src, '.')))
  591.     rptr = ptr;
  592.       slen = rptr - src;
  593.       strncpy (dst, src, slen);
  594.       dst[slen] = '\0';
  595.       if (*rptr == '.')
  596.     {
  597.       dst[slen++] = bracket;
  598.       dst[slen] = '\0';
  599.     }
  600.       else
  601.     {
  602.       /* If we have the top-level of a rooted directory (i.e. xx:[000000]),
  603.          then translate the device and recurse. */
  604.       if (dst[slen - 1] == ':'
  605.           && dst[slen - 2] != ':'    /* skip decnet nodes */
  606.           && strcmp(src + slen, "[000000]") == 0)
  607.         {
  608.           dst[slen - 1] = '\0';
  609.           if ((ptr = egetenv (dst))
  610.           && (rlen = strlen (ptr) - 1) > 0
  611.           && (ptr[rlen] == ']' || ptr[rlen] == '>')
  612.           && ptr[rlen - 1] == '.')
  613.         {
  614.           char * buf = (char *) alloca (strlen (ptr) + 1);
  615.           strcpy (buf, ptr);
  616.           buf[rlen - 1] = ']';
  617.           buf[rlen] = '\0';
  618.           return directory_file_name (buf, dst);
  619.         }
  620.           else
  621.         dst[slen - 1] = ':';
  622.         }
  623.       strcat (dst, "[000000]");
  624.       slen += 8;
  625.     }
  626.       rptr++;
  627.       rlen = strlen (rptr) - 1;
  628.       strncat (dst, rptr, rlen);
  629.       dst[slen + rlen] = '\0';
  630.       strcat (dst, ".DIR.1");
  631.       return 1;
  632.     }
  633. #endif /* VMS */
  634.   /* Process as Unix format: just remove any final slash.
  635.      But leave "/" unchanged; do not change it to "".  */
  636.   strcpy (dst, src);
  637.   if (slen > 1 
  638.       && dst[slen - 1] == '/'
  639. #if defined(MSDOS) || defined(AMIGA)
  640.       && dst[slen - 2] != ':'
  641. #endif
  642.       )
  643.     dst[slen - 1] = 0;
  644.   return 1;
  645. }
  646.  
  647. DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name,
  648.   1, 1, 0,
  649.   "Returns the file name of the directory named DIR.\n\
  650. This is the name of the file that holds the data for the directory DIR.\n\
  651. This operation exists because a directory is also a file, but its name as\n\
  652. a directory is different from its name as a file.\n\
  653. In Unix-syntax, this function just removes the final slash.\n\
  654. On VMS, given a VMS-syntax directory name such as \"[X.Y]\",\n\
  655. it returns a file name such as \"[X]Y.DIR.1\".")
  656.   (directory)
  657.      Lisp_Object directory;
  658. {
  659.   char *buf;
  660.   Lisp_Object handler;
  661.  
  662.   CHECK_STRING (directory, 0);
  663.  
  664.   if (NILP (directory))
  665.     return Qnil;
  666.  
  667.   /* If the file name has special constructs in it,
  668.      call the corresponding file handler.  */
  669.   handler = Ffind_file_name_handler (directory, Qdirectory_file_name);
  670.   if (!NILP (handler))
  671.     return call2 (handler, Qdirectory_file_name, directory);
  672.  
  673. #ifdef VMS
  674.   /* 20 extra chars is insufficient for VMS, since we might perform a
  675.      logical name translation. an equivalence string can be up to 255
  676.      chars long, so grab that much extra space...  - sss */
  677.   buf = (char *) alloca (XSTRING (directory)->size + 20 + 255);
  678. #else
  679.   buf = (char *) alloca (XSTRING (directory)->size + 20);
  680. #endif
  681.   directory_file_name (XSTRING (directory)->data, buf);
  682.   return build_string (buf);
  683. }
  684.  
  685. DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0,
  686.   "Generate temporary file name (string) starting with PREFIX (a string).\n\
  687. The Emacs process number forms part of the result,\n\
  688. so there is no danger of generating a name being used by another process.")
  689.   (prefix)
  690.      Lisp_Object prefix;
  691. {
  692.   Lisp_Object val;
  693.   val = concat2 (prefix, build_string ("XXXXXX"));
  694.   mktemp (XSTRING (val)->data);
  695.   return val;
  696. }
  697.  
  698. DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
  699.   "Convert FILENAME to absolute, and canonicalize it.\n\
  700. Second arg DEFAULT is directory to start with if FILENAME is relative\n\
  701.  (does not start with slash); if DEFAULT is nil or missing,\n\
  702. the current buffer's value of default-directory is used.\n\
  703. Path components that are `.' are removed, and \n\
  704. path components followed by `..' are removed, along with the `..' itself;\n\
  705. note that these simplifications are done without checking the resulting\n\
  706. paths in the file system.\n\
  707. An initial `~/' expands to your home directory.\n\
  708. An initial `~USER/' expands to USER's home directory.\n\
  709. See also the function `substitute-in-file-name'.")
  710.      (name, defalt)
  711.      Lisp_Object name, defalt;
  712. {
  713. #ifdef AMIGA
  714.   unsigned char *nm, *tilde, *newdir, *colon, *t_pos, *target;
  715.  
  716.   CHECK_STRING (name, 0);
  717.  
  718.   nm = XSTRING (name)->data;
  719.   /* Find base directory */
  720.   if (NILP (defalt))
  721.       defalt = current_buffer->directory;
  722.   CHECK_STRING (defalt, 1);
  723.   newdir = XSTRING (defalt)->data;
  724.  
  725.   /* Concat newdir w/ nm and canonicalize */
  726.   /* newdir always contains at least the device name.
  727.      It is assumed canonical */
  728.   target = (unsigned char *)alloca(strlen(nm) + strlen(newdir) + 2);
  729.   file_name_as_directory (target, newdir);
  730.   t_pos = target + strlen(target);
  731.  
  732.   while (*nm)
  733.   {
  734.       unsigned char *comp_end = nm;
  735.       int comp_len;
  736.  
  737.       /* Find next component of path (everything upto the next /) */
  738.       do comp_end++; while (comp_end[0] && comp_end[-1] != '/' && comp_end[-1] != ':');
  739.       comp_len = comp_end - nm;
  740.  
  741.       if (comp_len == 1 && nm[0] == '/' ||
  742.       nm[0] == '.' && nm[1] == '.' &&
  743.       (comp_len == 2 || comp_len == 3 && nm[2] == '/'))
  744.       {
  745.       /* Previous directory */
  746.       if (t_pos > target && t_pos[-1] != ':')
  747.       {
  748.           t_pos--; /* Back up over / */
  749.           while (t_pos > target &&
  750.              t_pos[-1] != ':' && t_pos[-1] != '/') t_pos--;
  751.       }
  752.       }
  753.       else if (comp_len == 2 && nm[0] == '.' && nm[1] == '/' ||
  754.            comp_len == 1 && nm[0] == '.') ; /* Ignore . */
  755.       else if (nm[0] == ':') /* Just keep disk name */
  756.       {
  757.       char *new_pos;
  758.  
  759.       *t_pos = 0; /* Limit search for : */
  760.       t_pos = index(target, ':');
  761.       if (t_pos) t_pos++;
  762.       else t_pos = target;
  763.       }
  764.       else if (nm[0] == '~' || index(nm, ':'))
  765.       {
  766.       char *exp_name;
  767.  
  768.       if (nm[0] == '~')
  769.           if (nm[1] == '/' || nm[1] == 0) /* Home directory */
  770.           {
  771.           newdir = (unsigned char *) egetenv ("HOME");
  772.           if (!newdir) newdir = (unsigned char *) "s:";
  773.           }
  774.           else
  775.           {
  776.           /* Handle ~ followed by user name.  */
  777.           char lastc = nm[comp_len - 1];
  778.           int len = comp_len - 1;
  779.  
  780.           if (lastc == ':' || lastc == '/') len--;
  781.  
  782.           /* ~name becomes name: */
  783.           newdir = (unsigned char *) alloca (len + 2);
  784.           bcopy((char *) nm + 1, newdir, len);
  785.           newdir[len] = ':';
  786.           newdir[len + 1] = 0;
  787.           }
  788.       else /* we have name: */
  789.       {
  790.           newdir = (char *)alloca(comp_len + 1);
  791.           bcopy(nm, newdir, comp_len);
  792.           newdir[comp_len] = 0;
  793.       }
  794.       exp_name = (char *)alloca(1024);
  795.       if (expand_path(newdir, exp_name, 1024))
  796.       {
  797.           char *colon = strchr(exp_name, ':');
  798.  
  799.           /* Detect paths with multiple colons (eg from PATH:) and
  800.          leave them alone. They create confusion. */
  801.           if (!(colon && strchr(colon + 1, ':'))) newdir = exp_name;
  802.       }
  803.       target = (unsigned char *)alloca(strlen(nm) + strlen(newdir) + 2);
  804.       file_name_as_directory (target, newdir);
  805.       t_pos = target + strlen(target);
  806.       }
  807.       else /* Copy component */
  808.       {
  809.       bcopy(nm, t_pos, comp_len);
  810.        t_pos += comp_len;
  811.       }
  812.  
  813.       nm = comp_end;
  814.   }
  815.   return make_string (target, t_pos - target);
  816. #else /* not AMIGA */
  817.   unsigned char *nm;
  818.   
  819.   register unsigned char *newdir, *p, *o;
  820.   int tlen;
  821.   unsigned char *target;
  822.   struct passwd *pw;
  823. #ifdef VMS
  824.   unsigned char * colon = 0;
  825.   unsigned char * close = 0;
  826.   unsigned char * slash = 0;
  827.   unsigned char * brack = 0;
  828.   int lbrack = 0, rbrack = 0;
  829.   int dots = 0;
  830. #endif /* VMS */
  831. #ifdef MSDOS    /* Demacs 1.1.2 91/10/20 Manabu Higashida */
  832.   int drive = -1;
  833.   int relpath = 0;
  834.   unsigned char *tmp, *defdir;
  835. #endif
  836.   Lisp_Object handler;
  837.   
  838.   CHECK_STRING (name, 0);
  839.  
  840.   /* If the file name has special constructs in it,
  841.      call the corresponding file handler.  */
  842.   handler = Ffind_file_name_handler (name, Qexpand_file_name);
  843.   if (!NILP (handler))
  844.     return call3 (handler, Qexpand_file_name, name, defalt);
  845.  
  846.   /* Use the buffer's default-directory if DEFALT is omitted.  */
  847.   if (NILP (defalt))
  848.     defalt = current_buffer->directory;
  849.   CHECK_STRING (defalt, 1);
  850.  
  851.   /* Make sure DEFALT is properly expanded.
  852.      It would be better to do this down below where we actually use
  853.      defalt.  Unfortunately, calling Fexpand_file_name recursively
  854.      could invoke GC, and the strings might be relocated.  This would
  855.      be annoying because we have pointers into strings lying around
  856.      that would need adjusting, and people would add new pointers to
  857.      the code and forget to adjust them, resulting in intermittent bugs.
  858.      Putting this call here avoids all that crud.
  859.  
  860.      The EQ test avoids infinite recursion.  */
  861.   if (! NILP (defalt) && !EQ (defalt, name)
  862.       /* This saves time in a common case.  */
  863.       && XSTRING (defalt)->data[0] != '/')
  864.     {
  865.       struct gcpro gcpro1;
  866.  
  867.       GCPRO1 (name);
  868.       defalt = Fexpand_file_name (defalt, Qnil);
  869.       UNGCPRO;
  870.     }
  871.  
  872. #ifdef VMS
  873.   /* Filenames on VMS are always upper case.  */
  874.   name = Fupcase (name);
  875. #endif
  876. #ifdef FILE_SYSTEM_CASE
  877.   name = FILE_SYSTEM_CASE (name);
  878. #endif
  879.  
  880.   nm = XSTRING (name)->data;
  881.   
  882. #ifdef MSDOS
  883.   /* firstly, strip drive name. */
  884.   {
  885.     unsigned char *colon = rindex (nm, ':');
  886.     if (colon)
  887.       if (nm == colon)
  888.     nm++;
  889.       else
  890.     {
  891.       drive = tolower (colon[-1]) - 'a';
  892.       nm = colon + 1;
  893.       if (*nm != '/')
  894.         {
  895.           defdir = alloca (MAXPATHLEN + 1);
  896.           relpath = getdefdir (drive + 1, defdir);
  897.         }
  898.     }    
  899.   }
  900. #endif
  901.  
  902.   /* If nm is absolute, flush ...// and detect /./ and /../.
  903.      If no /./ or /../ we can return right away. */
  904.   if (
  905.       nm[0] == '/'
  906. #ifdef VMS
  907.       || index (nm, ':')
  908. #endif /* VMS */
  909.       )
  910.     {
  911.       /* If it turns out that the filename we want to return is just a
  912.      suffix of FILENAME, we don't need to go through and edit
  913.      things; we just need to construct a new string using data
  914.      starting at the middle of FILENAME.  If we set lose to a
  915.      non-zero value, that means we've discovered that we can't do
  916.      that cool trick.  */
  917.       int lose = 0;
  918.  
  919.       p = nm;
  920.       while (*p)
  921.     {
  922.       /* Since we know the path is absolute, we can assume that each
  923.          element starts with a "/".  */
  924.  
  925.       /* "//" anywhere isn't necessarily hairy; we just start afresh
  926.          with the second slash.  */
  927.       if (p[0] == '/' && p[1] == '/'
  928. #ifdef APOLLO
  929.           /* // at start of filename is meaningful on Apollo system */
  930.           && nm != p
  931. #endif /* APOLLO */
  932.           )
  933.         nm = p + 1;
  934.  
  935.       /* "~" is hairy as the start of any path element.  */
  936.       if (p[0] == '/' && p[1] == '~')
  937.         nm = p + 1, lose = 1;
  938.  
  939.       /* "." and ".." are hairy.  */
  940.       if (p[0] == '/'
  941.           && p[1] == '.'
  942.           && (p[2] == '/'
  943.           || p[2] == 0
  944.           || (p[2] == '.' && (p[3] == '/'
  945.                       || p[3] == 0))))
  946.         lose = 1;
  947. #ifdef VMS
  948.       if (p[0] == '\\')
  949.         lose = 1;
  950.       if (p[0] == '/') {
  951.         /* if dev:[dir]/, move nm to / */
  952.         if (!slash && p > nm && (brack || colon)) {
  953.           nm = (brack ? brack + 1 : colon + 1);
  954.           lbrack = rbrack = 0;
  955.           brack = 0;
  956.           colon = 0;
  957.         }
  958.         slash = p;
  959.       }
  960.       if (p[0] == '-')
  961. #ifndef VMS4_4
  962.         /* VMS pre V4.4,convert '-'s in filenames. */
  963.         if (lbrack == rbrack)
  964.           {
  965.         if (dots < 2)    /* this is to allow negative version numbers */
  966.           p[0] = '_';
  967.           }
  968.         else
  969. #endif /* VMS4_4 */
  970.           if (lbrack > rbrack &&
  971.           ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
  972.            (p[1] == '.' || p[1] == ']' || p[1] == '>')))
  973.         lose = 1;
  974. #ifndef VMS4_4
  975.           else
  976.         p[0] = '_';
  977. #endif /* VMS4_4 */
  978.       /* count open brackets, reset close bracket pointer */
  979.       if (p[0] == '[' || p[0] == '<')
  980.         lbrack++, brack = 0;
  981.       /* count close brackets, set close bracket pointer */
  982.       if (p[0] == ']' || p[0] == '>')
  983.         rbrack++, brack = p;
  984.       /* detect ][ or >< */
  985.       if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<'))
  986.         lose = 1;
  987.       if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~')
  988.         nm = p + 1, lose = 1;
  989.       if (p[0] == ':' && (colon || slash))
  990.         /* if dev1:[dir]dev2:, move nm to dev2: */
  991.         if (brack)
  992.           {
  993.         nm = brack + 1;
  994.         brack = 0;
  995.           }
  996.         /* if /pathname/dev:, move nm to dev: */
  997.         else if (slash)
  998.           nm = slash + 1;
  999.         /* if node::dev:, move colon following dev */
  1000.         else if (colon && colon[-1] == ':')
  1001.           colon = p;
  1002.         /* if dev1:dev2:, move nm to dev2: */
  1003.         else if (colon && colon[-1] != ':')
  1004.           {
  1005.         nm = colon + 1;
  1006.         colon = 0;
  1007.           }
  1008.       if (p[0] == ':' && !colon)
  1009.         {
  1010.           if (p[1] == ':')
  1011.         p++;
  1012.           colon = p;
  1013.         }
  1014.       if (lbrack == rbrack)
  1015.         if (p[0] == ';')
  1016.           dots = 2;
  1017.         else if (p[0] == '.')
  1018.           dots++;
  1019. #endif /* VMS */
  1020.       p++;
  1021.     }
  1022.       if (!lose)
  1023.     {
  1024. #ifdef VMS
  1025.       if (index (nm, '/'))
  1026.         return build_string (sys_translate_unix (nm));
  1027. #endif /* VMS */
  1028. #ifndef MSDOS
  1029.       if (nm == XSTRING (name)->data)
  1030.         return name;
  1031.       return build_string (nm);
  1032. #endif
  1033.     }
  1034.     }
  1035.  
  1036.   /* Now determine directory to start with and put it in newdir */
  1037.  
  1038.   newdir = 0;
  1039.  
  1040.   if (nm[0] == '~')        /* prefix ~ */
  1041.     {
  1042.       if (nm[1] == '/'
  1043. #ifdef VMS
  1044.       || nm[1] == ':'
  1045. #endif                /* VMS */
  1046.       || nm[1] == 0)    /* ~ by itself */
  1047.     {
  1048.       if (!(newdir = (unsigned char *) egetenv ("HOME")))
  1049.         newdir = (unsigned char *) "";
  1050. #ifdef MSDOS
  1051.       dostounix_filename (newdir);
  1052. #endif
  1053.       nm++;
  1054. #ifdef VMS
  1055.       nm++;            /* Don't leave the slash in nm.  */
  1056. #endif                /* VMS */
  1057.     }
  1058.       else            /* ~user/filename */
  1059.     {
  1060.       for (p = nm; *p && (*p != '/'
  1061. #ifdef VMS
  1062.                   && *p != ':'
  1063. #endif                /* VMS */
  1064.                   ); p++);
  1065.       o = (unsigned char *) alloca (p - nm + 1);
  1066.       bcopy ((char *) nm, o, p - nm);
  1067.       o [p - nm] = 0;
  1068.  
  1069.       pw = (struct passwd *) getpwnam (o + 1);
  1070.       if (pw)
  1071.         {
  1072.           newdir = (unsigned char *) pw -> pw_dir;
  1073. #ifdef VMS
  1074.           nm = p + 1;    /* skip the terminator */
  1075. #else
  1076.           nm = p;
  1077. #endif                /* VMS */
  1078.         }
  1079.  
  1080.       /* If we don't find a user of that name, leave the name
  1081.          unchanged; don't move nm forward to p.  */
  1082.     }
  1083.     }
  1084.  
  1085.   if (nm[0] != '/'
  1086. #ifdef VMS
  1087.       && !index (nm, ':')
  1088. #endif /* not VMS */
  1089. #ifdef MSDOS
  1090.       && drive == -1
  1091. #endif
  1092.       && !newdir)
  1093.     {
  1094.       newdir = XSTRING (defalt)->data;
  1095.     }
  1096.  
  1097. #ifdef MSDOS
  1098.   if (newdir == 0 && relpath)
  1099.     newdir = defdir; 
  1100. #endif
  1101.   if (newdir != 0)
  1102.     {
  1103.       /* Get rid of any slash at the end of newdir.  */
  1104.       int length = strlen (newdir);
  1105.       /* Adding `length > 1 &&' makes ~ expand into / when homedir
  1106.      is the root dir.  People disagree about whether that is right.
  1107.      Anyway, we can't take the risk of this change now.  */
  1108. #ifdef MSDOS
  1109.       if (newdir[1] != ':' && length > 1)
  1110. #endif
  1111.       if (newdir[length - 1] == '/')
  1112.     {
  1113.       unsigned char *temp = (unsigned char *) alloca (length);
  1114.       bcopy (newdir, temp, length - 1);
  1115.       temp[length - 1] = 0;
  1116.       newdir = temp;
  1117.     }
  1118.       tlen = length + 1;
  1119.     }
  1120.   else
  1121.     tlen = 0;
  1122.  
  1123.   /* Now concatenate the directory and name to new space in the stack frame */
  1124.   tlen += strlen (nm) + 1;
  1125. #ifdef MSDOS
  1126.   /* Add reserved space for drive name.  */
  1127.   target = (unsigned char *) alloca (tlen + 2) + 2;
  1128. #else
  1129.   target = (unsigned char *) alloca (tlen);
  1130. #endif
  1131.   *target = 0;
  1132.  
  1133.   if (newdir)
  1134.     {
  1135. #ifndef VMS
  1136.       if (nm[0] == 0 || nm[0] == '/')
  1137.     strcpy (target, newdir);
  1138.       else
  1139. #endif
  1140.     file_name_as_directory (target, newdir);
  1141.     }
  1142.  
  1143.   strcat (target, nm);
  1144. #ifdef VMS
  1145.   if (index (target, '/'))
  1146.     strcpy (target, sys_translate_unix (target));
  1147. #endif /* VMS */
  1148.  
  1149.   /* Now canonicalize by removing /. and /foo/.. if they appear.  */
  1150.  
  1151.   p = target;
  1152.   o = target;
  1153.  
  1154.   while (*p)
  1155.     {
  1156. #ifdef VMS
  1157.       if (*p != ']' && *p != '>' && *p != '-')
  1158.     {
  1159.       if (*p == '\\')
  1160.         p++;
  1161.       *o++ = *p++;
  1162.     }
  1163.       else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2)
  1164.     /* brackets are offset from each other by 2 */
  1165.     {
  1166.       p += 2;
  1167.       if (*p != '.' && *p != '-' && o[-1] != '.')
  1168.         /* convert [foo][bar] to [bar] */
  1169.         while (o[-1] != '[' && o[-1] != '<')
  1170.           o--;
  1171.       else if (*p == '-' && *o != '.')
  1172.         *--p = '.';
  1173.     }
  1174.       else if (p[0] == '-' && o[-1] == '.' &&
  1175.            (p[1] == '.' || p[1] == ']' || p[1] == '>'))
  1176.     /* flush .foo.- ; leave - if stopped by '[' or '<' */
  1177.     {
  1178.       do
  1179.         o--;
  1180.       while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<');
  1181.       if (p[1] == '.')    /* foo.-.bar ==> bar*/
  1182.         p += 2;
  1183.       else if (o[-1] == '.') /* '.foo.-]' ==> ']' */
  1184.         p++, o--;
  1185.       /* else [foo.-] ==> [-] */
  1186.     }
  1187.       else
  1188.     {
  1189. #ifndef VMS4_4
  1190.       if (*p == '-' &&
  1191.           o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
  1192.           p[1] != ']' && p[1] != '>' && p[1] != '.')
  1193.         *p = '_';
  1194. #endif /* VMS4_4 */
  1195.       *o++ = *p++;
  1196.     }
  1197. #else /* not VMS */
  1198.       if (*p != '/')
  1199.      {
  1200.       *o++ = *p++;
  1201.     }
  1202.       else if (!strncmp (p, "//", 2)
  1203. #ifdef APOLLO
  1204.            /* // at start of filename is meaningful in Apollo system */
  1205.            && o != target
  1206. #endif /* APOLLO */
  1207.            )
  1208.     {
  1209.       o = target;
  1210.       p++;
  1211.     }
  1212.       else if (p[0] == '/'
  1213.            && p[1] == '.'
  1214.            && (p[2] == '/'
  1215.            || p[2] == 0))
  1216.     {
  1217.       /* If "/." is the entire filename, keep the "/".  Otherwise,
  1218.          just delete the whole "/.".  */
  1219.       if (o == target && p[2] == '\0')
  1220.         *o++ = *p;
  1221.       p += 2;
  1222.     }
  1223.       else if (!strncmp (p, "/..", 3)
  1224.            /* `/../' is the "superroot" on certain file systems.  */
  1225.            && o != target
  1226.            && (p[3] == '/' || p[3] == 0))
  1227.     {
  1228.       while (o != target && *--o != '/')
  1229.         ;
  1230. #ifdef APOLLO
  1231.       if (o == target + 1 && o[-1] == '/' && o[0] == '/')
  1232.         ++o;
  1233.       else
  1234. #endif /* APOLLO */
  1235.       if (o == target && *o == '/')
  1236.         ++o;
  1237.       p += 3;
  1238.     }
  1239.       else
  1240.      {
  1241.       *o++ = *p++;
  1242.     }
  1243. #endif /* not VMS */
  1244.     }
  1245.  
  1246. #ifdef MSDOS
  1247.   /* at last, set drive name. */
  1248.   if (target[1] != ':')
  1249.     {
  1250.       target -= 2;
  1251.       target[0] = (drive < 0 ? getdisk () : drive) + 'a';
  1252.       target[1] = ':';
  1253.     }
  1254. #endif
  1255.  
  1256.   return make_string (target, o - target);
  1257. #endif /* not AMIGA */
  1258. }
  1259. #if 0
  1260. /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'.
  1261. DEAFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
  1262.   "Convert FILENAME to absolute, and canonicalize it.\n\
  1263. Second arg DEFAULT is directory to start with if FILENAME is relative\n\
  1264.  (does not start with slash); if DEFAULT is nil or missing,\n\
  1265. the current buffer's value of default-directory is used.\n\
  1266. Filenames containing `.' or `..' as components are simplified;\n\
  1267. initial `~/' expands to your home directory.\n\
  1268. See also the function `substitute-in-file-name'.")
  1269.      (name, defalt)
  1270.      Lisp_Object name, defalt;
  1271. {
  1272.   unsigned char *nm;
  1273.   
  1274.   register unsigned char *newdir, *p, *o;
  1275.   int tlen;
  1276.   unsigned char *target;
  1277.   struct passwd *pw;
  1278.   int lose;
  1279. #ifdef VMS
  1280.   unsigned char * colon = 0;
  1281.   unsigned char * close = 0;
  1282.   unsigned char * slash = 0;
  1283.   unsigned char * brack = 0;
  1284.   int lbrack = 0, rbrack = 0;
  1285.   int dots = 0;
  1286. #endif /* VMS */
  1287.   
  1288.   CHECK_STRING (name, 0);
  1289.  
  1290. #ifdef VMS
  1291.   /* Filenames on VMS are always upper case.  */
  1292.   name = Fupcase (name);
  1293. #endif
  1294.  
  1295.   nm = XSTRING (name)->data;
  1296.   
  1297.   /* If nm is absolute, flush ...// and detect /./ and /../.
  1298.      If no /./ or /../ we can return right away. */
  1299.   if (
  1300.       nm[0] == '/'
  1301. #ifdef VMS
  1302.       || index (nm, ':')
  1303. #endif /* VMS */
  1304.       )
  1305.     {
  1306.       p = nm;
  1307.       lose = 0;
  1308.       while (*p)
  1309.     {
  1310.       if (p[0] == '/' && p[1] == '/'
  1311. #ifdef APOLLO
  1312.           /* // at start of filename is meaningful on Apollo system */
  1313.           && nm != p
  1314. #endif /* APOLLO */
  1315.           )
  1316.         nm = p + 1;
  1317.       if (p[0] == '/' && p[1] == '~')
  1318.         nm = p + 1, lose = 1;
  1319.       if (p[0] == '/' && p[1] == '.'
  1320.           && (p[2] == '/' || p[2] == 0
  1321.           || (p[2] == '.' && (p[3] == '/' || p[3] == 0))))
  1322.         lose = 1;
  1323. #ifdef VMS
  1324.       if (p[0] == '\\')
  1325.         lose = 1;
  1326.       if (p[0] == '/') {
  1327.         /* if dev:[dir]/, move nm to / */
  1328.         if (!slash && p > nm && (brack || colon)) {
  1329.           nm = (brack ? brack + 1 : colon + 1);
  1330.           lbrack = rbrack = 0;
  1331.           brack = 0;
  1332.           colon = 0;
  1333.         }
  1334.         slash = p;
  1335.       }
  1336.       if (p[0] == '-')
  1337. #ifndef VMS4_4
  1338.         /* VMS pre V4.4,convert '-'s in filenames. */
  1339.         if (lbrack == rbrack)
  1340.           {
  1341.         if (dots < 2)    /* this is to allow negative version numbers */
  1342.           p[0] = '_';
  1343.           }
  1344.         else
  1345. #endif /* VMS4_4 */
  1346.           if (lbrack > rbrack &&
  1347.           ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
  1348.            (p[1] == '.' || p[1] == ']' || p[1] == '>')))
  1349.         lose = 1;
  1350. #ifndef VMS4_4
  1351.           else
  1352.         p[0] = '_';
  1353. #endif /* VMS4_4 */
  1354.       /* count open brackets, reset close bracket pointer */
  1355.       if (p[0] == '[' || p[0] == '<')
  1356.         lbrack++, brack = 0;
  1357.       /* count close brackets, set close bracket pointer */
  1358.       if (p[0] == ']' || p[0] == '>')
  1359.         rbrack++, brack = p;
  1360.       /* detect ][ or >< */
  1361.       if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<'))
  1362.         lose = 1;
  1363.       if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~')
  1364.         nm = p + 1, lose = 1;
  1365.       if (p[0] == ':' && (colon || slash))
  1366.         /* if dev1:[dir]dev2:, move nm to dev2: */
  1367.         if (brack)
  1368.           {
  1369.         nm = brack + 1;
  1370.         brack = 0;
  1371.           }
  1372.         /* if /pathname/dev:, move nm to dev: */
  1373.         else if (slash)
  1374.           nm = slash + 1;
  1375.         /* if node::dev:, move colon following dev */
  1376.         else if (colon && colon[-1] == ':')
  1377.           colon = p;
  1378.         /* if dev1:dev2:, move nm to dev2: */
  1379.         else if (colon && colon[-1] != ':')
  1380.           {
  1381.         nm = colon + 1;
  1382.         colon = 0;
  1383.           }
  1384.       if (p[0] == ':' && !colon)
  1385.         {
  1386.           if (p[1] == ':')
  1387.         p++;
  1388.           colon = p;
  1389.         }
  1390.       if (lbrack == rbrack)
  1391.         if (p[0] == ';')
  1392.           dots = 2;
  1393.         else if (p[0] == '.')
  1394.           dots++;
  1395. #endif /* VMS */
  1396.       p++;
  1397.     }
  1398.       if (!lose)
  1399.     {
  1400. #ifdef VMS
  1401.       if (index (nm, '/'))
  1402.         return build_string (sys_translate_unix (nm));
  1403. #endif /* VMS */
  1404.       if (nm == XSTRING (name)->data)
  1405.         return name;
  1406.       return build_string (nm);
  1407.     }
  1408.     }
  1409.  
  1410.   /* Now determine directory to start with and put it in NEWDIR */
  1411.  
  1412.   newdir = 0;
  1413.  
  1414.   if (nm[0] == '~')        /* prefix ~ */
  1415.     if (nm[1] == '/'
  1416. #ifdef VMS
  1417.     || nm[1] == ':'
  1418. #endif /* VMS */
  1419.     || nm[1] == 0)/* ~/filename */
  1420.       {
  1421.     if (!(newdir = (unsigned char *) egetenv ("HOME")))
  1422.       newdir = (unsigned char *) "";
  1423.     nm++;
  1424. #ifdef VMS
  1425.     nm++;            /* Don't leave the slash in nm.  */
  1426. #endif /* VMS */
  1427.       }
  1428.     else  /* ~user/filename */
  1429.       {
  1430.     /* Get past ~ to user */
  1431.     unsigned char *user = nm + 1;
  1432.     /* Find end of name. */
  1433.     unsigned char *ptr = (unsigned char *) index (user, '/');
  1434.     int len = ptr ? ptr - user : strlen (user);
  1435. #ifdef VMS
  1436.     unsigned char *ptr1 = index (user, ':');
  1437.     if (ptr1 != 0 && ptr1 - user < len)
  1438.       len = ptr1 - user;
  1439. #endif                /* VMS */
  1440.     /* Copy the user name into temp storage. */
  1441.     o = (unsigned char *) alloca (len + 1);
  1442.     bcopy ((char *) user, o, len);
  1443.     o[len] = 0;
  1444.  
  1445.     /* Look up the user name. */
  1446.     pw = (struct passwd *) getpwnam (o + 1);
  1447.     if (!pw)
  1448.       error ("\"%s\" isn't a registered user", o + 1);
  1449.  
  1450.     newdir = (unsigned char *) pw->pw_dir;
  1451.  
  1452.     /* Discard the user name from NM.  */
  1453.     nm += len;
  1454.       }
  1455.  
  1456.   if (nm[0] != '/'
  1457. #ifdef VMS
  1458.       && !index (nm, ':')
  1459. #endif /* not VMS */
  1460.       && !newdir)
  1461.     {
  1462.       if (NILP (defalt))
  1463.     defalt = current_buffer->directory;
  1464.       CHECK_STRING (defalt, 1);
  1465.       newdir = XSTRING (defalt)->data;
  1466.     }
  1467.  
  1468.   /* Now concatenate the directory and name to new space in the stack frame */
  1469.  
  1470.   tlen = (newdir ? strlen (newdir) + 1 : 0) + strlen (nm) + 1;
  1471.   target = (unsigned char *) alloca (tlen);
  1472.   *target = 0;
  1473.  
  1474.   if (newdir)
  1475.     {
  1476. #ifndef VMS
  1477.       if (nm[0] == 0 || nm[0] == '/')
  1478.     strcpy (target, newdir);
  1479.       else
  1480. #endif
  1481.       file_name_as_directory (target, newdir);
  1482.     }
  1483.  
  1484.   strcat (target, nm);
  1485. #ifdef VMS
  1486.   if (index (target, '/'))
  1487.     strcpy (target, sys_translate_unix (target));
  1488. #endif /* VMS */
  1489.  
  1490.   /* Now canonicalize by removing /. and /foo/.. if they appear */
  1491.  
  1492.   p = target;
  1493.   o = target;
  1494.  
  1495.   while (*p)
  1496.     {
  1497. #ifdef VMS
  1498.       if (*p != ']' && *p != '>' && *p != '-')
  1499.     {
  1500.       if (*p == '\\')
  1501.         p++;
  1502.       *o++ = *p++;
  1503.     }
  1504.       else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2)
  1505.     /* brackets are offset from each other by 2 */
  1506.     {
  1507.       p += 2;
  1508.       if (*p != '.' && *p != '-' && o[-1] != '.')
  1509.         /* convert [foo][bar] to [bar] */
  1510.         while (o[-1] != '[' && o[-1] != '<')
  1511.           o--;
  1512.       else if (*p == '-' && *o != '.')
  1513.         *--p = '.';
  1514.     }
  1515.       else if (p[0] == '-' && o[-1] == '.' &&
  1516.            (p[1] == '.' || p[1] == ']' || p[1] == '>'))
  1517.     /* flush .foo.- ; leave - if stopped by '[' or '<' */
  1518.     {
  1519.       do
  1520.         o--;
  1521.       while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<');
  1522.       if (p[1] == '.')    /* foo.-.bar ==> bar*/
  1523.         p += 2;
  1524.       else if (o[-1] == '.') /* '.foo.-]' ==> ']' */
  1525.         p++, o--;
  1526.       /* else [foo.-] ==> [-] */
  1527.     }
  1528.       else
  1529.     {
  1530. #ifndef VMS4_4
  1531.       if (*p == '-' &&
  1532.           o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
  1533.           p[1] != ']' && p[1] != '>' && p[1] != '.')
  1534.         *p = '_';
  1535. #endif /* VMS4_4 */
  1536.       *o++ = *p++;
  1537.     }
  1538. #else /* not VMS */
  1539.       if (*p != '/')
  1540.      {
  1541.       *o++ = *p++;
  1542.     }
  1543.       else if (!strncmp (p, "//", 2)
  1544. #ifdef APOLLO
  1545.            /* // at start of filename is meaningful in Apollo system */
  1546.            && o != target
  1547. #endif /* APOLLO */
  1548.            )
  1549.     {
  1550.       o = target;
  1551.       p++;
  1552.     }
  1553.       else if (p[0] == '/' && p[1] == '.' &&
  1554.            (p[2] == '/' || p[2] == 0))
  1555.     p += 2;
  1556.       else if (!strncmp (p, "/..", 3)
  1557.            /* `/../' is the "superroot" on certain file systems.  */
  1558.            && o != target
  1559.            && (p[3] == '/' || p[3] == 0))
  1560.     {
  1561.       while (o != target && *--o != '/')
  1562.         ;
  1563. #ifdef APOLLO
  1564.       if (o == target + 1 && o[-1] == '/' && o[0] == '/')
  1565.         ++o;
  1566.       else
  1567. #endif /* APOLLO */
  1568.       if (o == target && *o == '/')
  1569.         ++o;
  1570.       p += 3;
  1571.     }
  1572.       else
  1573.      {
  1574.       *o++ = *p++;
  1575.     }
  1576. #endif /* not VMS */
  1577.     }
  1578.  
  1579.   return make_string (target, o - target);
  1580. }
  1581. #endif
  1582.  
  1583. DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name,
  1584.   Ssubstitute_in_file_name, 1, 1, 0,
  1585.   "Substitute environment variables referred to in FILENAME.\n\
  1586. `$FOO' where FOO is an environment variable name means to substitute\n\
  1587. the value of that variable.  The variable name should be terminated\n\
  1588. with a character not a letter, digit or underscore; otherwise, enclose\n\
  1589. the entire variable name in braces.\n\
  1590. If `/~' appears, all of FILENAME through that `/' is discarded.\n\n\
  1591. On VMS, `$' substitution is not done; this function does little and only\n\
  1592. duplicates what `expand-file-name' does.")
  1593.   (string)
  1594.      Lisp_Object string;
  1595. {
  1596.   unsigned char *nm;
  1597.  
  1598.   register unsigned char *s, *p, *o, *x, *endp;
  1599.   unsigned char *target;
  1600.   int total = 0;
  1601.   int substituted = 0;
  1602.   unsigned char *xnm;
  1603.  
  1604.   CHECK_STRING (string, 0);
  1605.  
  1606.   nm = XSTRING (string)->data;
  1607.   endp = nm + XSTRING (string)->size;
  1608.  
  1609.   /* If /~ or // appears, discard everything through first slash. */
  1610.  
  1611.   for (p = nm; p != endp; p++)
  1612.     {
  1613. #ifdef AMIGA /* CHFIXME: check */
  1614.       if (p[0] == '~' && p != nm && p[-1] == '/')
  1615.     {
  1616.         nm = p;
  1617.         substituted = 1;
  1618.     }
  1619.       else if (p[0] == ':')
  1620.     {
  1621.           char *p2 = p;
  1622.       while (p2 > nm && p2[-1] != ':' && p2[-1] != '/') p2--;
  1623.       if (p2 != nm)
  1624.         {
  1625.           nm = p2;
  1626.           substituted = 1;
  1627.         }
  1628.     }
  1629. #else /* not AMIGA */
  1630.       if ((p[0] == '~' ||
  1631. #ifdef APOLLO
  1632.        /* // at start of file name is meaningful in Apollo system */
  1633.        (p[0] == '/' && p - 1 != nm)
  1634. #else /* not APOLLO */
  1635.        p[0] == '/'
  1636. #endif /* not APOLLO */
  1637.        )
  1638.       && p != nm &&
  1639. #ifdef VMS
  1640.       (p[-1] == ':' || p[-1] == ']' || p[-1] == '>' ||
  1641. #endif /* VMS */
  1642.       p[-1] == '/')
  1643. #ifdef VMS
  1644.       )
  1645. #endif /* VMS */
  1646.     {
  1647.       nm = p;
  1648.       substituted = 1;
  1649.     }
  1650. #ifdef MSDOS
  1651.       if (p[0] && p[1] == ':')
  1652.     {
  1653.       nm = p;
  1654.       substituted = 1;
  1655.     }
  1656. #endif /* MSDOS */
  1657. #endif /* not AMIGA */
  1658.     }
  1659.  
  1660. #ifdef VMS
  1661.   return build_string (nm);
  1662. #else
  1663.  
  1664.   /* See if any variables are substituted into the string
  1665.      and find the total length of their values in `total' */
  1666.  
  1667.   for (p = nm; p != endp;)
  1668.     if (*p != '$')
  1669.       p++;
  1670.     else
  1671.       {
  1672.     p++;
  1673.     if (p == endp)
  1674.       goto badsubst;
  1675.     else if (*p == '$')
  1676.       {
  1677.         /* "$$" means a single "$" */
  1678.         p++;
  1679.         total -= 1;
  1680.         substituted = 1;
  1681.         continue;
  1682.       }
  1683.     else if (*p == '{')
  1684.       {
  1685.         o = ++p;
  1686.         while (p != endp && *p != '}') p++;
  1687.         if (*p != '}') goto missingclose;
  1688.         s = p;
  1689.       }
  1690.     else
  1691.       {
  1692.         o = p;
  1693.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1694.         s = p;
  1695.       }
  1696.  
  1697.     /* Copy out the variable name */
  1698.     target = (unsigned char *) alloca (s - o + 1);
  1699.     strncpy (target, o, s - o);
  1700.     target[s - o] = 0;
  1701. #ifdef MSDOS
  1702.     strupr (target); /* $home == $HOME etc.  */
  1703. #endif
  1704.  
  1705.     /* Get variable value */
  1706.     o = (unsigned char *) egetenv (target);
  1707.     if (!o) goto badvar;
  1708.     total += strlen (o);
  1709.     substituted = 1;
  1710.       }
  1711.  
  1712.   if (!substituted)
  1713.     return string;
  1714.  
  1715.   /* If substitution required, recopy the string and do it */
  1716.   /* Make space in stack frame for the new copy */
  1717.   xnm = (unsigned char *) alloca (XSTRING (string)->size + total + 1);
  1718.   x = xnm;
  1719.  
  1720.   /* Copy the rest of the name through, replacing $ constructs with values */
  1721.   for (p = nm; *p;)
  1722.     if (*p != '$')
  1723.       *x++ = *p++;
  1724.     else
  1725.       {
  1726.     p++;
  1727.     if (p == endp)
  1728.       goto badsubst;
  1729.     else if (*p == '$')
  1730.       {
  1731.         *x++ = *p++;
  1732.         continue;
  1733.       }
  1734.     else if (*p == '{')
  1735.       {
  1736.         o = ++p;
  1737.         while (p != endp && *p != '}') p++;
  1738.         if (*p != '}') goto missingclose;
  1739.         s = p++;
  1740.       }
  1741.     else
  1742.       {
  1743.         o = p;
  1744.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1745.         s = p;
  1746.       }
  1747.  
  1748.     /* Copy out the variable name */
  1749.     target = (unsigned char *) alloca (s - o + 1);
  1750.     strncpy (target, o, s - o);
  1751.     target[s - o] = 0;
  1752. #ifdef MSDOS
  1753.     strupr (target); /* $home == $HOME etc.  */
  1754. #endif
  1755.  
  1756.     /* Get variable value */
  1757.     o = (unsigned char *) egetenv (target);
  1758.     if (!o)
  1759.       goto badvar;
  1760.  
  1761.     strcpy (x, o);
  1762.     x += strlen (o);
  1763.       }
  1764.  
  1765.   *x = 0;
  1766.  
  1767.   /* If /~ or // appears, discard everything through first slash. */
  1768.  
  1769.   for (p = xnm; p != x; p++)
  1770.     if ((p[0] == '~' ||
  1771. #ifdef APOLLO
  1772.      /* // at start of file name is meaningful in Apollo system */
  1773.      (p[0] == '/' && p - 1 != xnm)
  1774. #else /* not APOLLO */
  1775.      p[0] == '/'
  1776. #endif /* not APOLLO */
  1777.      )
  1778.     && p != nm && p[-1] == '/')
  1779.       xnm = p;
  1780. #ifdef MSDOS
  1781.     else if (p[0] && p[1] == ':')
  1782.     xnm = p;
  1783. #endif
  1784.  
  1785.   return make_string (xnm, x - xnm);
  1786.  
  1787.  badsubst:
  1788.   error ("Bad format environment-variable substitution");
  1789.  missingclose:
  1790.   error ("Missing \"}\" in environment-variable substitution");
  1791.  badvar:
  1792.   error ("Substituting nonexistent environment variable \"%s\"", target);
  1793.  
  1794.   /* NOTREACHED */
  1795. #endif /* not VMS */
  1796. }
  1797.  
  1798. /* A slightly faster and more convenient way to get
  1799.    (directory-file-name (expand-file-name FOO)).  */
  1800.  
  1801. Lisp_Object
  1802. expand_and_dir_to_file (filename, defdir)
  1803.      Lisp_Object filename, defdir;
  1804. {
  1805.   register Lisp_Object abspath;
  1806.  
  1807.   abspath = Fexpand_file_name (filename, defdir);
  1808. #ifdef VMS
  1809.   {
  1810.     register int c = XSTRING (abspath)->data[XSTRING (abspath)->size - 1];
  1811.     if (c == ':' || c == ']' || c == '>')
  1812.       abspath = Fdirectory_file_name (abspath);
  1813.   }
  1814. #else
  1815.   /* Remove final slash, if any (unless path is root).
  1816.      stat behaves differently depending!  */
  1817.   if (XSTRING (abspath)->size > 1
  1818.       && XSTRING (abspath)->data[XSTRING (abspath)->size - 1] == '/')
  1819.     /* We cannot take shortcuts; they might be wrong for magic file names.  */
  1820.     abspath = Fdirectory_file_name (abspath);
  1821. #endif
  1822.   return abspath;
  1823. }
  1824.  
  1825. barf_or_query_if_file_exists (absname, querystring, interactive)
  1826.      Lisp_Object absname;
  1827.      unsigned char *querystring;
  1828.      int interactive;
  1829. {
  1830.   register Lisp_Object tem;
  1831.   struct gcpro gcpro1;
  1832.  
  1833.   if (access (XSTRING (absname)->data, 4) >= 0)
  1834.     {
  1835.       if (! interactive)
  1836.     Fsignal (Qfile_already_exists,
  1837.          Fcons (build_string ("File already exists"),
  1838.             Fcons (absname, Qnil)));
  1839.       GCPRO1 (absname);
  1840.       tem = do_yes_or_no_p (format1 ("File %s already exists; %s anyway? ",
  1841.                      XSTRING (absname)->data, querystring));
  1842.       UNGCPRO;
  1843.       if (NILP (tem))
  1844.     Fsignal (Qfile_already_exists,
  1845.          Fcons (build_string ("File already exists"),
  1846.             Fcons (absname, Qnil)));
  1847.     }
  1848.   return;
  1849. }
  1850.  
  1851. DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4,
  1852.   "fCopy file: \nFCopy %s to file: \np\nP",
  1853.   "Copy FILE to NEWNAME.  Both args must be strings.\n\
  1854. Signals a `file-already-exists' error if file NEWNAME already exists,\n\
  1855. unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.\n\
  1856. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1857. This is what happens in interactive use with M-x.\n\
  1858. Fourth arg KEEP-TIME non-nil means give the new file the same\n\
  1859. last-modified time as the old one.  (This works on only some systems.)\n\
  1860. A prefix arg makes KEEP-TIME non-nil.")
  1861.   (filename, newname, ok_if_already_exists, keep_date)
  1862.      Lisp_Object filename, newname, ok_if_already_exists, keep_date;
  1863. {
  1864.   int ifd, ofd, n;
  1865.   char buf[16 * 1024];
  1866.   struct stat st;
  1867.   Lisp_Object handler;
  1868.   struct gcpro gcpro1, gcpro2;
  1869.   int count = specpdl_ptr - specpdl;
  1870.   Lisp_Object args[6];
  1871.   int input_file_statable_p;
  1872.  
  1873.   GCPRO2 (filename, newname);
  1874.   CHECK_STRING (filename, 0);
  1875.   CHECK_STRING (newname, 1);
  1876.   filename = Fexpand_file_name (filename, Qnil);
  1877.   newname = Fexpand_file_name (newname, Qnil);
  1878.  
  1879.   /* If the input file name has special constructs in it,
  1880.      call the corresponding file handler.  */
  1881.   handler = Ffind_file_name_handler (filename, Qcopy_file);
  1882.   /* Likewise for output file name.  */
  1883.   if (NILP (handler))
  1884.     handler = Ffind_file_name_handler (newname, Qcopy_file);
  1885.   if (!NILP (handler))
  1886.     RETURN_UNGCPRO (call5 (handler, Qcopy_file, filename, newname,
  1887.                ok_if_already_exists, keep_date));
  1888.  
  1889.   if (NILP (ok_if_already_exists)
  1890.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1891.     barf_or_query_if_file_exists (newname, "copy to it",
  1892.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1893.  
  1894.   ifd = open (XSTRING (filename)->data, 0);
  1895.   if (ifd < 0)
  1896.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1897.  
  1898.   record_unwind_protect (close_file_unwind, make_number (ifd));
  1899.  
  1900.   /* We can only copy regular files and symbolic links.  Other files are not
  1901.      copyable by us. */
  1902.   input_file_statable_p = (fstat (ifd, &st) >= 0);
  1903.  
  1904. #if defined (S_ISREG) && defined (S_ISLNK)
  1905.   if (input_file_statable_p)
  1906.     {
  1907.       if (!(S_ISREG (st.st_mode)) && !(S_ISLNK (st.st_mode)))
  1908.     {
  1909. #if defined (EISDIR)
  1910.       /* Get a better looking error message. */
  1911.       errno = EISDIR;
  1912. #endif /* EISDIR */
  1913.     report_file_error ("Non-regular file", Fcons (filename, Qnil));
  1914.     }
  1915.     }
  1916. #endif /* S_ISREG && S_ISLNK */
  1917.  
  1918. #ifdef VMS
  1919.   /* Create the copy file with the same record format as the input file */
  1920.   ofd = sys_creat (XSTRING (newname)->data, 0666, ifd);
  1921. #else
  1922. #ifdef MSDOS
  1923.   /* System's default file type was set to binary by _fmode in emacs.c.  */
  1924.   ofd = creat (XSTRING (newname)->data, S_IREAD | S_IWRITE);
  1925. #else /* not MSDOS */
  1926.   ofd = creat (XSTRING (newname)->data, 0666);
  1927. #endif /* not MSDOS */
  1928. #endif /* VMS */
  1929.   if (ofd < 0)
  1930.       report_file_error ("Opening output file", Fcons (newname, Qnil));
  1931.  
  1932.   record_unwind_protect (close_file_unwind, make_number (ofd));
  1933.  
  1934.   immediate_quit = 1;
  1935.   QUIT;
  1936.   while ((n = read (ifd, buf, sizeof buf)) > 0)
  1937.     if (write (ofd, buf, n) != n)
  1938.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1939.   immediate_quit = 0;
  1940.  
  1941.   /* Closing the output clobbers the file times on some systems.  */
  1942.   if (close (ofd) < 0)
  1943.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1944.  
  1945.   if (input_file_statable_p)
  1946.     {
  1947.       if (!NILP (keep_date))
  1948.     {
  1949.       EMACS_TIME atime, mtime;
  1950.       EMACS_SET_SECS_USECS (atime, st.st_atime, 0);
  1951.       EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
  1952.       EMACS_SET_UTIMES (XSTRING (newname)->data, atime, mtime);
  1953.     }
  1954. #ifdef APOLLO
  1955.       if (!egetenv ("USE_DOMAIN_ACLS"))
  1956. #endif
  1957.     chmod (XSTRING (newname)->data, st.st_mode & 07777);
  1958.     }
  1959.  
  1960.   close (ifd);
  1961.  
  1962.   /* Discard the unwind protects.  */
  1963.   specpdl_ptr = specpdl + count;
  1964.  
  1965.   UNGCPRO;
  1966.   return Qnil;
  1967. }
  1968.  
  1969. DEFUN ("make-directory-internal", Fmake_directory_internal,
  1970.        Smake_directory_internal, 1, 1, 0,
  1971.   "Create a directory.  One argument, a file name string.")
  1972.   (dirname)
  1973.      Lisp_Object dirname;
  1974. {
  1975.   unsigned char *dir;
  1976.   Lisp_Object handler;
  1977.  
  1978.   CHECK_STRING (dirname, 0);
  1979.   dirname = Fexpand_file_name (dirname, Qnil);
  1980.  
  1981.   handler = Ffind_file_name_handler (dirname, Qmake_directory);
  1982.   if (!NILP (handler))
  1983.     return call3 (handler, Qmake_directory, dirname, Qnil);
  1984.  
  1985.   dir = XSTRING (dirname)->data;
  1986.  
  1987.   if (mkdir (dir, 0777) != 0)
  1988.     report_file_error ("Creating directory", Flist (1, &dirname));
  1989.  
  1990.   return Qnil;
  1991. }
  1992.  
  1993. DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete directory: ",
  1994.   "Delete a directory.  One argument, a file name or directory name string.")
  1995.   (dirname)
  1996.      Lisp_Object dirname;
  1997. {
  1998.   unsigned char *dir;
  1999.   Lisp_Object handler;
  2000.  
  2001.   CHECK_STRING (dirname, 0);
  2002.   dirname = Fdirectory_file_name (Fexpand_file_name (dirname, Qnil));
  2003.   dir = XSTRING (dirname)->data;
  2004.  
  2005.   handler = Ffind_file_name_handler (dirname, Qdelete_directory);
  2006.   if (!NILP (handler))
  2007.     return call2 (handler, Qdelete_directory, dirname);
  2008.  
  2009.   if (rmdir (dir) != 0)
  2010.     report_file_error ("Removing directory", Flist (1, &dirname));
  2011.  
  2012.   return Qnil;
  2013. }
  2014.  
  2015. DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: ",
  2016.   "Delete specified file.  One argument, a file name string.\n\
  2017. If file has multiple names, it continues to exist with the other names.")
  2018.   (filename)
  2019.      Lisp_Object filename;
  2020. {
  2021.   Lisp_Object handler;
  2022.   CHECK_STRING (filename, 0);
  2023.   filename = Fexpand_file_name (filename, Qnil);
  2024.  
  2025.   handler = Ffind_file_name_handler (filename, Qdelete_file);
  2026.   if (!NILP (handler))
  2027.     return call2 (handler, Qdelete_file, filename);
  2028.  
  2029.   if (0 > unlink (XSTRING (filename)->data))
  2030.     report_file_error ("Removing old name", Flist (1, &filename));
  2031.   return Qnil;
  2032. }
  2033.  
  2034. DEFUN ("rename-file", Frename_file, Srename_file, 2, 3,
  2035.   "fRename file: \nFRename %s to file: \np",
  2036.   "Rename FILE as NEWNAME.  Both args strings.\n\
  2037. If file has names other than FILE, it continues to have those names.\n\
  2038. Signals a `file-already-exists' error if a file NEWNAME already exists\n\
  2039. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  2040. A number as third arg means request confirmation if NEWNAME already exists.\n\
  2041. This is what happens in interactive use with M-x.")
  2042.   (filename, newname, ok_if_already_exists)
  2043.      Lisp_Object filename, newname, ok_if_already_exists;
  2044. {
  2045. #ifdef NO_ARG_ARRAY
  2046.   Lisp_Object args[2];
  2047. #endif
  2048.   Lisp_Object handler;
  2049.   struct gcpro gcpro1, gcpro2;
  2050.  
  2051.   GCPRO2 (filename, newname);
  2052.   CHECK_STRING (filename, 0);
  2053.   CHECK_STRING (newname, 1);
  2054.   filename = Fexpand_file_name (filename, Qnil);
  2055.   newname = Fexpand_file_name (newname, Qnil);
  2056.  
  2057.   /* If the file name has special constructs in it,
  2058.      call the corresponding file handler.  */
  2059.   handler = Ffind_file_name_handler (filename, Qrename_file);
  2060.   if (NILP (handler))
  2061.     handler = Ffind_file_name_handler (newname, Qrename_file);
  2062.   if (!NILP (handler))
  2063.     RETURN_UNGCPRO (call4 (handler, Qrename_file,
  2064.                filename, newname, ok_if_already_exists));
  2065.  
  2066.   if (NILP (ok_if_already_exists)
  2067.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  2068.     barf_or_query_if_file_exists (newname, "rename to it",
  2069.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  2070. #ifndef BSD4_1
  2071.   if (0 > rename (XSTRING (filename)->data, XSTRING (newname)->data))
  2072. #else
  2073.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data)
  2074.       || 0 > unlink (XSTRING (filename)->data))
  2075. #endif
  2076.     {
  2077.       if (errno == EXDEV)
  2078.     {
  2079.       Fcopy_file (filename, newname,
  2080.               /* We have already prompted if it was an integer,
  2081.              so don't have copy-file prompt again.  */
  2082.               NILP (ok_if_already_exists) ? Qnil : Qt, Qt);
  2083.       Fdelete_file (filename);
  2084.     }
  2085.       else
  2086. #ifdef NO_ARG_ARRAY
  2087.     {
  2088.       args[0] = filename;
  2089.       args[1] = newname;
  2090.       report_file_error ("Renaming", Flist (2, args));
  2091.     }
  2092. #else
  2093.     report_file_error ("Renaming", Flist (2, &filename));
  2094. #endif
  2095.     }
  2096.   UNGCPRO;
  2097.   return Qnil;
  2098. }
  2099.  
  2100. DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3,
  2101.   "fAdd name to file: \nFName to add to %s: \np",
  2102.   "Give FILE additional name NEWNAME.  Both args strings.\n\
  2103. Signals a `file-already-exists' error if a file NEWNAME already exists\n\
  2104. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  2105. A number as third arg means request confirmation if NEWNAME already exists.\n\
  2106. This is what happens in interactive use with M-x.")
  2107.   (filename, newname, ok_if_already_exists)
  2108.      Lisp_Object filename, newname, ok_if_already_exists;
  2109. {
  2110. #ifdef NO_ARG_ARRAY
  2111.   Lisp_Object args[2];
  2112. #endif
  2113.   Lisp_Object handler;
  2114.   struct gcpro gcpro1, gcpro2;
  2115.  
  2116.   GCPRO2 (filename, newname);
  2117.   CHECK_STRING (filename, 0);
  2118.   CHECK_STRING (newname, 1);
  2119.   filename = Fexpand_file_name (filename, Qnil);
  2120.   newname = Fexpand_file_name (newname, Qnil);
  2121.  
  2122.   /* If the file name has special constructs in it,
  2123.      call the corresponding file handler.  */
  2124.   handler = Ffind_file_name_handler (filename, Qadd_name_to_file);
  2125.   if (!NILP (handler))
  2126.     RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, filename,
  2127.                newname, ok_if_already_exists));
  2128.  
  2129.   if (NILP (ok_if_already_exists)
  2130.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  2131.     barf_or_query_if_file_exists (newname, "make it a new name",
  2132.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  2133.   unlink (XSTRING (newname)->data);
  2134.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data))
  2135.     {
  2136. #ifdef NO_ARG_ARRAY
  2137.       args[0] = filename;
  2138.       args[1] = newname;
  2139.       report_file_error ("Adding new name", Flist (2, args));
  2140. #else
  2141.       report_file_error ("Adding new name", Flist (2, &filename));
  2142. #endif
  2143.     }
  2144.  
  2145.   UNGCPRO;
  2146.   return Qnil;
  2147. }
  2148.  
  2149. #ifdef S_IFLNK
  2150. DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3,
  2151.   "FMake symbolic link to file: \nFMake symbolic link to file %s: \np",
  2152.   "Make a symbolic link to FILENAME, named LINKNAME.  Both args strings.\n\
  2153. Signals a `file-already-exists' error if a file NEWNAME already exists\n\
  2154. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  2155. A number as third arg means request confirmation if NEWNAME already exists.\n\
  2156. This happens for interactive use with M-x.")
  2157.   (filename, linkname, ok_if_already_exists)
  2158.      Lisp_Object filename, linkname, ok_if_already_exists;
  2159. {
  2160. #ifdef NO_ARG_ARRAY
  2161.   Lisp_Object args[2];
  2162. #endif
  2163.   Lisp_Object handler;
  2164.   struct gcpro gcpro1, gcpro2;
  2165.  
  2166.   GCPRO2 (filename, linkname);
  2167.   CHECK_STRING (filename, 0);
  2168.   CHECK_STRING (linkname, 1);
  2169.   /* If the link target has a ~, we must expand it to get
  2170.      a truly valid file name.  Otherwise, do not expand;
  2171.      we want to permit links to relative file names.  */
  2172.   if (XSTRING (filename)->data[0] == '~')
  2173.     filename = Fexpand_file_name (filename, Qnil);
  2174.   linkname = Fexpand_file_name (linkname, Qnil);
  2175.  
  2176.   /* If the file name has special constructs in it,
  2177.      call the corresponding file handler.  */
  2178.   handler = Ffind_file_name_handler (filename, Qmake_symbolic_link);
  2179.   if (!NILP (handler))
  2180.     RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename,
  2181.                linkname, ok_if_already_exists));
  2182.  
  2183.   if (NILP (ok_if_already_exists)
  2184.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  2185.     barf_or_query_if_file_exists (linkname, "make it a link",
  2186.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  2187.   if (0 > symlink (XSTRING (filename)->data, XSTRING (linkname)->data))
  2188.     {
  2189.       /* If we didn't complain already, silently delete existing file.  */
  2190.       if (errno == EEXIST)
  2191.     {
  2192.       unlink (XSTRING (linkname)->data);
  2193.       if (0 <= symlink (XSTRING (filename)->data, XSTRING (linkname)->data))
  2194.         return Qnil;
  2195.     }
  2196.  
  2197. #ifdef NO_ARG_ARRAY
  2198.       args[0] = filename;
  2199.       args[1] = linkname;
  2200.       report_file_error ("Making symbolic link", Flist (2, args));
  2201. #else
  2202.       report_file_error ("Making symbolic link", Flist (2, &filename));
  2203. #endif
  2204.     }
  2205.   UNGCPRO;
  2206.   return Qnil;
  2207. }
  2208. #endif /* S_IFLNK */
  2209.  
  2210. #ifdef VMS
  2211.  
  2212. DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name,
  2213.        2, 2, "sDefine logical name: \nsDefine logical name %s as: ",
  2214.   "Define the job-wide logical name NAME to have the value STRING.\n\
  2215. If STRING is nil or a null string, the logical name NAME is deleted.")
  2216.   (varname, string)
  2217.      Lisp_Object varname;
  2218.      Lisp_Object string;
  2219. {
  2220.   CHECK_STRING (varname, 0);
  2221.   if (NILP (string))
  2222.     delete_logical_name (XSTRING (varname)->data);
  2223.   else
  2224.     {
  2225.       CHECK_STRING (string, 1);
  2226.  
  2227.       if (XSTRING (string)->size == 0)
  2228.         delete_logical_name (XSTRING (varname)->data);
  2229.       else
  2230.         define_logical_name (XSTRING (varname)->data, XSTRING (string)->data);
  2231.     }
  2232.  
  2233.   return string;
  2234. }
  2235. #endif /* VMS */
  2236.  
  2237. #ifdef HPUX_NET
  2238.  
  2239. DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0,
  2240.        "Open a network connection to PATH using LOGIN as the login string.")
  2241.      (path, login)
  2242.      Lisp_Object path, login;
  2243. {
  2244.   int netresult;
  2245.   
  2246.   CHECK_STRING (path, 0);
  2247.   CHECK_STRING (login, 0);  
  2248.   
  2249.   netresult = netunam (XSTRING (path)->data, XSTRING (login)->data);
  2250.  
  2251.   if (netresult == -1)
  2252.     return Qnil;
  2253.   else
  2254.     return Qt;
  2255. }
  2256. #endif /* HPUX_NET */
  2257.  
  2258. DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p,
  2259.        1, 1, 0,
  2260.        "Return t if file FILENAME specifies an absolute path name.\n\
  2261. On Unix, this is a name starting with a `/' or a `~'.")
  2262.      (filename)
  2263.      Lisp_Object filename;
  2264. {
  2265.   unsigned char *ptr;
  2266.  
  2267.   CHECK_STRING (filename, 0);
  2268.   ptr = XSTRING (filename)->data;
  2269. #ifdef    AMIGA
  2270.   /* An absolute filename has a non-leading ':' in it */
  2271.   if (*ptr != ':')
  2272.       while (*ptr)
  2273.       if (*ptr++ == ':') return Qt;
  2274.   return Qnil;
  2275. #else    /* not AMIGA */
  2276.   if (*ptr == '/' || *ptr == '~'
  2277. #ifdef VMS
  2278. /* ??? This criterion is probably wrong for '<'.  */
  2279.       || index (ptr, ':') || index (ptr, '<')
  2280.       || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']'))
  2281.       && ptr[1] != '.')
  2282. #endif /* VMS */
  2283. #ifdef MSDOS
  2284.       || (*ptr != 0 && ptr[1] == ':' && ptr[2] == '/')
  2285. #endif
  2286.       )
  2287.     return Qt;
  2288.   else
  2289.     return Qnil;
  2290. #endif /* not AMIGA */
  2291. }
  2292.  
  2293. DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0,
  2294.   "Return t if file FILENAME exists.  (This does not mean you can read it.)\n\
  2295. See also `file-readable-p' and `file-attributes'.")
  2296.   (filename)
  2297.      Lisp_Object filename;
  2298. {
  2299.   Lisp_Object abspath;
  2300.   Lisp_Object handler;
  2301.  
  2302.   CHECK_STRING (filename, 0);
  2303.   abspath = Fexpand_file_name (filename, Qnil);
  2304.  
  2305.   /* If the file name has special constructs in it,
  2306.      call the corresponding file handler.  */
  2307.   handler = Ffind_file_name_handler (abspath, Qfile_exists_p);
  2308.   if (!NILP (handler))
  2309.     return call2 (handler, Qfile_exists_p, abspath);
  2310.  
  2311.   return (access (XSTRING (abspath)->data, 0) >= 0) ? Qt : Qnil;
  2312. }
  2313.  
  2314. DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0,
  2315.   "Return t if FILENAME can be executed by you.\n\
  2316. For a directory, this means you can access files in that directory.")
  2317.   (filename)
  2318.     Lisp_Object filename;
  2319.  
  2320. {
  2321.   Lisp_Object abspath;
  2322.   Lisp_Object handler;
  2323.  
  2324.   CHECK_STRING (filename, 0);
  2325.   abspath = Fexpand_file_name (filename, Qnil);
  2326.  
  2327.   /* If the file name has special constructs in it,
  2328.      call the corresponding file handler.  */
  2329.   handler = Ffind_file_name_handler (abspath, Qfile_executable_p);
  2330.   if (!NILP (handler))
  2331.     return call2 (handler, Qfile_executable_p, abspath);
  2332.  
  2333.   return (access (XSTRING (abspath)->data, 1) >= 0) ? Qt : Qnil;
  2334. }
  2335.  
  2336. DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0,
  2337.   "Return t if file FILENAME exists and you can read it.\n\
  2338. See also `file-exists-p' and `file-attributes'.")
  2339.   (filename)
  2340.      Lisp_Object filename;
  2341. {
  2342.   Lisp_Object abspath;
  2343.   Lisp_Object handler;
  2344.  
  2345.   CHECK_STRING (filename, 0);
  2346.   abspath = Fexpand_file_name (filename, Qnil);
  2347.  
  2348.   /* If the file name has special constructs in it,
  2349.      call the corresponding file handler.  */
  2350.   handler = Ffind_file_name_handler (abspath, Qfile_readable_p);
  2351.   if (!NILP (handler))
  2352.     return call2 (handler, Qfile_readable_p, abspath);
  2353.  
  2354.   return (access (XSTRING (abspath)->data, 4) >= 0) ? Qt : Qnil;
  2355. }
  2356.  
  2357. DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0,
  2358.   "Return non-nil if file FILENAME is the name of a symbolic link.\n\
  2359. The value is the name of the file to which it is linked.\n\
  2360. Otherwise returns nil.")
  2361.   (filename)
  2362.      Lisp_Object filename;
  2363. {
  2364. #ifdef S_IFLNK
  2365.   char *buf;
  2366.   int bufsize;
  2367.   int valsize;
  2368.   Lisp_Object val;
  2369.   Lisp_Object handler;
  2370.  
  2371.   CHECK_STRING (filename, 0);
  2372.   filename = Fexpand_file_name (filename, Qnil);
  2373.  
  2374.   /* If the file name has special constructs in it,
  2375.      call the corresponding file handler.  */
  2376.   handler = Ffind_file_name_handler (filename, Qfile_symlink_p);
  2377.   if (!NILP (handler))
  2378.     return call2 (handler, Qfile_symlink_p, filename);
  2379.  
  2380.   bufsize = 100;
  2381.   while (1)
  2382.     {
  2383.       buf = (char *) xmalloc (bufsize);
  2384.       bzero (buf, bufsize);
  2385.       valsize = readlink (XSTRING (filename)->data, buf, bufsize);
  2386.       if (valsize < bufsize) break;
  2387.       /* Buffer was not long enough */
  2388.       xfree (buf);
  2389.       bufsize *= 2;
  2390.     }
  2391.   /* OK.  This is required on the Amiga, but I suspect that it makes
  2392.    * sense to not accept a NULL link.  This should probably be merged in,
  2393.    * but that is someone else's decision.
  2394.    */
  2395. #ifndef AMIGA
  2396.   if (valsize == -1)
  2397. #else
  2398.   if (valsize < 0)
  2399. #endif
  2400.     {
  2401.       xfree (buf);
  2402.       return Qnil;
  2403.     }
  2404.   val = make_string (buf, valsize);
  2405.   xfree (buf);
  2406.   return val;
  2407. #else /* not S_IFLNK */
  2408.   return Qnil;
  2409. #endif /* not S_IFLNK */
  2410. }
  2411.  
  2412. #ifdef SOLARIS_BROKEN_ACCESS
  2413. /* In Solaris 2.1, the readonly-ness of the filesystem is not
  2414.    considered by the access system call.  This is Sun's bug, but we
  2415.    still have to make Emacs work.  */
  2416.  
  2417. #include <sys/statvfs.h>
  2418.  
  2419. static int
  2420. ro_fsys (path)
  2421.     char *path;
  2422. {
  2423.     struct statvfs statvfsb;
  2424.  
  2425.     if (statvfs(path, &statvfsb))
  2426.       return 1;  /* error from statvfs, be conservative and say not wrtable */
  2427.     else
  2428.       /* Otherwise, fsys is ro if bit is set.  */
  2429.       return statvfsb.f_flag & ST_RDONLY;
  2430. }
  2431. #else
  2432. /* But on every other os, access has already done the right thing.  */
  2433. #define ro_fsys(path) 0
  2434. #endif
  2435.  
  2436. /* Having this before file-symlink-p mysteriously caused it to be forgotten
  2437.    on the RT/PC.  */
  2438. DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
  2439.   "Return t if file FILENAME can be written or created by you.")
  2440.   (filename)
  2441.      Lisp_Object filename;
  2442. {
  2443.   Lisp_Object abspath, dir;
  2444.   Lisp_Object handler;
  2445.  
  2446.   CHECK_STRING (filename, 0);
  2447.   abspath = Fexpand_file_name (filename, Qnil);
  2448.  
  2449.   /* If the file name has special constructs in it,
  2450.      call the corresponding file handler.  */
  2451.   handler = Ffind_file_name_handler (abspath, Qfile_writable_p);
  2452.   if (!NILP (handler))
  2453.     return call2 (handler, Qfile_writable_p, abspath);
  2454.  
  2455.   if (access (XSTRING (abspath)->data, 0) >= 0)
  2456.     return ((access (XSTRING (abspath)->data, 2) >= 0
  2457.          && ! ro_fsys ((char *) XSTRING (abspath)->data))
  2458.         ? Qt : Qnil);
  2459.   dir = Ffile_name_directory (abspath);
  2460. #ifdef VMS
  2461.   if (!NILP (dir))
  2462.     dir = Fdirectory_file_name (dir);
  2463. #endif /* VMS */
  2464. #ifdef MSDOS
  2465.   if (!NILP (dir))
  2466.     dir = Fdirectory_file_name (dir);
  2467. #endif /* MSDOS */
  2468.   return ((access (!NILP (dir) ? (char *) XSTRING (dir)->data : "", 2) >= 0
  2469.        && ! ro_fsys ((char *) XSTRING (dir)->data))
  2470.       ? Qt : Qnil);
  2471. }
  2472.  
  2473. DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0,
  2474.   "Return t if file FILENAME is the name of a directory as a file.\n\
  2475. A directory name spec may be given instead; then the value is t\n\
  2476. if the directory so specified exists and really is a directory.")
  2477.   (filename)
  2478.      Lisp_Object filename;
  2479. {
  2480.   register Lisp_Object abspath;
  2481.   struct stat st;
  2482.   Lisp_Object handler;
  2483.  
  2484.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  2485.  
  2486.   /* If the file name has special constructs in it,
  2487.      call the corresponding file handler.  */
  2488.   handler = Ffind_file_name_handler (abspath, Qfile_directory_p);
  2489.   if (!NILP (handler))
  2490.     return call2 (handler, Qfile_directory_p, abspath);
  2491.  
  2492.   if (stat (XSTRING (abspath)->data, &st) < 0)
  2493.     return Qnil;
  2494.   return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
  2495. }
  2496.  
  2497. DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, Sfile_accessible_directory_p, 1, 1, 0,
  2498.   "Return t if file FILENAME is the name of a directory as a file,\n\
  2499. and files in that directory can be opened by you.  In order to use a\n\
  2500. directory as a buffer's current directory, this predicate must return true.\n\
  2501. A directory name spec may be given instead; then the value is t\n\
  2502. if the directory so specified exists and really is a readable and\n\
  2503. searchable directory.")
  2504.   (filename)
  2505.      Lisp_Object filename;
  2506. {
  2507.   Lisp_Object handler;
  2508.  
  2509.   /* If the file name has special constructs in it,
  2510.      call the corresponding file handler.  */
  2511.   handler = Ffind_file_name_handler (filename, Qfile_accessible_directory_p);
  2512.   if (!NILP (handler))
  2513.     return call2 (handler, Qfile_accessible_directory_p, filename);
  2514.  
  2515.   if (NILP (Ffile_directory_p (filename))
  2516.       || NILP (Ffile_executable_p (filename)))
  2517.     return Qnil;
  2518.   else
  2519.     return Qt;
  2520. }
  2521.  
  2522. DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0,
  2523.   "Return mode bits of FILE, as an integer.")
  2524.   (filename)
  2525.      Lisp_Object filename;
  2526. {
  2527.   Lisp_Object abspath;
  2528.   struct stat st;
  2529.   Lisp_Object handler;
  2530.  
  2531.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  2532.  
  2533.   /* If the file name has special constructs in it,
  2534.      call the corresponding file handler.  */
  2535.   handler = Ffind_file_name_handler (abspath, Qfile_modes);
  2536.   if (!NILP (handler))
  2537.     return call2 (handler, Qfile_modes, abspath);
  2538.  
  2539.   if (stat (XSTRING (abspath)->data, &st) < 0)
  2540.     return Qnil;
  2541. #ifdef MSDOS
  2542.   {
  2543.     int len;
  2544.     char *suffix;
  2545.     if (S_ISREG (st.st_mode)
  2546.     && (len = XSTRING (abspath)->size) >= 5
  2547.     && (stricmp ((suffix = XSTRING (abspath)->data + len-4), ".com") == 0
  2548.         || stricmp (suffix, ".exe") == 0
  2549.         || stricmp (suffix, ".bat") == 0))
  2550.       st.st_mode |= S_IEXEC;
  2551.   }
  2552. #endif /* MSDOS */
  2553.  
  2554.   return make_number (st.st_mode & 07777);
  2555. }
  2556.  
  2557. DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0,
  2558.   "Set mode bits of FILE to MODE (an integer).\n\
  2559. Only the 12 low bits of MODE are used.")
  2560.   (filename, mode)
  2561.      Lisp_Object filename, mode;
  2562. {
  2563.   Lisp_Object abspath;
  2564.   Lisp_Object handler;
  2565.  
  2566.   abspath = Fexpand_file_name (filename, current_buffer->directory);
  2567.   CHECK_NUMBER (mode, 1);
  2568.  
  2569.   /* If the file name has special constructs in it,
  2570.      call the corresponding file handler.  */
  2571.   handler = Ffind_file_name_handler (abspath, Qset_file_modes);
  2572.   if (!NILP (handler))
  2573.     return call3 (handler, Qset_file_modes, abspath, mode);
  2574.  
  2575. #ifndef APOLLO
  2576.   if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  2577.     report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  2578. #else /* APOLLO */
  2579.   if (!egetenv ("USE_DOMAIN_ACLS"))
  2580.     {
  2581.       struct stat st;
  2582.       struct timeval tvp[2];
  2583.  
  2584.       /* chmod on apollo also change the file's modtime; need to save the
  2585.      modtime and then restore it. */
  2586.       if (stat (XSTRING (abspath)->data, &st) < 0)
  2587.     {
  2588.       report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  2589.       return (Qnil);
  2590.     }
  2591.  
  2592.       if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  2593.      report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  2594.  
  2595.       /* reset the old accessed and modified times.  */
  2596.       tvp[0].tv_sec = st.st_atime + 1; /* +1 due to an Apollo roundoff bug */
  2597.       tvp[0].tv_usec = 0;
  2598.       tvp[1].tv_sec = st.st_mtime + 1; /* +1 due to an Apollo roundoff bug */
  2599.       tvp[1].tv_usec = 0;
  2600.  
  2601.       if (utimes (XSTRING (abspath)->data, tvp) < 0)
  2602.      report_file_error ("Doing utimes", Fcons (abspath, Qnil));
  2603.     }
  2604. #endif /* APOLLO */
  2605.  
  2606.   return Qnil;
  2607. }
  2608.  
  2609. DEFUN ("set-default-file-modes", Fset_default_file_modes, Sset_default_file_modes, 1, 1, 0,
  2610.     "Set the file permission bits for newly created files.\n\
  2611. The argument MODE should be an integer; only the low 9 bits are used.\n\
  2612. This setting is inherited by subprocesses.")
  2613.   (mode)
  2614.      Lisp_Object mode;
  2615. {
  2616.   CHECK_NUMBER (mode, 0);
  2617.   
  2618.   umask ((~ XINT (mode)) & 0777);
  2619.  
  2620.   return Qnil;
  2621. }
  2622.  
  2623. DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0,
  2624.     "Return the default file protection for created files.\n\
  2625. The value is an integer.")
  2626.   ()
  2627. {
  2628.   int realmask;
  2629.   Lisp_Object value;
  2630.  
  2631.   realmask = umask (0);
  2632.   umask (realmask);
  2633.  
  2634.   XSET (value, Lisp_Int, (~ realmask) & 0777);
  2635.   return value;
  2636. }
  2637.  
  2638. #ifdef unix
  2639.  
  2640. DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "",
  2641.   "Tell Unix to finish all pending disk updates.")
  2642.   ()
  2643. {
  2644.   sync ();
  2645.   return Qnil;
  2646. }
  2647.  
  2648. #endif /* unix */
  2649.  
  2650. DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0,
  2651.   "Return t if file FILE1 is newer than file FILE2.\n\
  2652. If FILE1 does not exist, the answer is nil;\n\
  2653. otherwise, if FILE2 does not exist, the answer is t.")
  2654.   (file1, file2)
  2655.      Lisp_Object file1, file2;
  2656. {
  2657.   Lisp_Object abspath1, abspath2;
  2658.   struct stat st;
  2659.   int mtime1;
  2660.   Lisp_Object handler;
  2661.   struct gcpro gcpro1, gcpro2;
  2662.  
  2663.   CHECK_STRING (file1, 0);
  2664.   CHECK_STRING (file2, 0);
  2665.  
  2666.   abspath1 = Qnil;
  2667.   GCPRO2 (abspath1, file2);
  2668.   abspath1 = expand_and_dir_to_file (file1, current_buffer->directory);
  2669.   abspath2 = expand_and_dir_to_file (file2, current_buffer->directory);
  2670.   UNGCPRO;
  2671.  
  2672.   /* If the file name has special constructs in it,
  2673.      call the corresponding file handler.  */
  2674.   handler = Ffind_file_name_handler (abspath1, Qfile_newer_than_file_p);
  2675.   if (NILP (handler))
  2676.     handler = Ffind_file_name_handler (abspath2, Qfile_newer_than_file_p);
  2677.   if (!NILP (handler))
  2678.     return call3 (handler, Qfile_newer_than_file_p, abspath1, abspath2);
  2679.  
  2680.   if (stat (XSTRING (abspath1)->data, &st) < 0)
  2681.     return Qnil;
  2682.  
  2683.   mtime1 = st.st_mtime;
  2684.  
  2685.   if (stat (XSTRING (abspath2)->data, &st) < 0)
  2686.     return Qt;
  2687.  
  2688.   return (mtime1 > st.st_mtime) ? Qt : Qnil;
  2689. }
  2690.  
  2691. #ifdef MSDOS
  2692. Lisp_Object Qfind_buffer_file_type;
  2693. #endif
  2694.  
  2695. DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
  2696.   1, 5, 0,
  2697.   "Insert contents of file FILENAME after point.\n\
  2698. Returns list of absolute file name and length of data inserted.\n\
  2699. If second argument VISIT is non-nil, the buffer's visited filename\n\
  2700. and last save file modtime are set, and it is marked unmodified.\n\
  2701. If visiting and the file does not exist, visiting is completed\n\
  2702. before the error is signaled.\n\n\
  2703. The optional third and fourth arguments BEG and END\n\
  2704. specify what portion of the file to insert.\n\
  2705. If VISIT is non-nil, BEG and END must be nil.\n\
  2706. If optional fifth argument REPLACE is non-nil,\n\
  2707. it means replace the current buffer contents (in the accessible portion)\n\
  2708. with the file contents.  This is better than simply deleting and inserting\n\
  2709. the whole thing because (1) it preserves some marker positions\n\
  2710. and (2) it puts less data in the undo list.")
  2711.   (filename, visit, beg, end, replace)
  2712.      Lisp_Object filename, visit, beg, end, replace;
  2713. {
  2714.   struct stat st;
  2715.   register int fd;
  2716.   register int inserted = 0;
  2717.   register int how_much;
  2718.   int count = specpdl_ptr - specpdl;
  2719.   struct gcpro gcpro1, gcpro2;
  2720.   Lisp_Object handler, val, insval;
  2721.   Lisp_Object p;
  2722.   int total;
  2723.  
  2724.   val = Qnil;
  2725.   p = Qnil;
  2726.  
  2727.   GCPRO2 (filename, p);
  2728.   if (!NILP (current_buffer->read_only))
  2729.     Fbarf_if_buffer_read_only();
  2730.  
  2731.   CHECK_STRING (filename, 0);
  2732.   filename = Fexpand_file_name (filename, Qnil);
  2733.  
  2734.   /* If the file name has special constructs in it,
  2735.      call the corresponding file handler.  */
  2736.   handler = Ffind_file_name_handler (filename, Qinsert_file_contents);
  2737.   if (!NILP (handler))
  2738.     {
  2739.       val = call6 (handler, Qinsert_file_contents, filename,
  2740.            visit, beg, end, replace);
  2741.       goto handled;
  2742.     }
  2743.  
  2744.   fd = -1;
  2745.  
  2746. #ifndef APOLLO
  2747.   if (stat (XSTRING (filename)->data, &st) < 0
  2748.       || (fd = open (XSTRING (filename)->data, 0)) < 0)
  2749. #else
  2750.   if ((fd = open (XSTRING (filename)->data, 0)) < 0
  2751.       || fstat (fd, &st) < 0)
  2752. #endif /* not APOLLO */
  2753.     {
  2754.       if (fd >= 0) close (fd);
  2755.       if (NILP (visit))
  2756.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  2757.       st.st_mtime = -1;
  2758.       how_much = 0;
  2759.       goto notfound;
  2760.     }
  2761.  
  2762.   /* Replacement should preserve point as it preserves markers.  */
  2763.   if (!NILP (replace))
  2764.     record_unwind_protect (restore_point_unwind, Fpoint_marker ());
  2765.  
  2766.   record_unwind_protect (close_file_unwind, make_number (fd));
  2767.  
  2768. #ifdef S_IFSOCK
  2769.   /* This code will need to be changed in order to work on named
  2770.      pipes, and it's probably just not worth it.  So we should at
  2771.      least signal an error.  */
  2772.   if ((st.st_mode & S_IFMT) == S_IFSOCK)
  2773.     Fsignal (Qfile_error,
  2774.          Fcons (build_string ("reading from named pipe"),
  2775.             Fcons (filename, Qnil)));
  2776. #endif
  2777.  
  2778.   /* Supposedly happens on VMS.  */
  2779.   if (st.st_size < 0)
  2780.     error ("File size is negative");
  2781.  
  2782.   if (!NILP (beg) || !NILP (end))
  2783.     if (!NILP (visit))
  2784.       error ("Attempt to visit less than an entire file");
  2785.  
  2786.   if (!NILP (beg))
  2787.     CHECK_NUMBER (beg, 0);
  2788.   else
  2789.     XFASTINT (beg) = 0;
  2790.  
  2791.   if (!NILP (end))
  2792.     CHECK_NUMBER (end, 0);
  2793.   else
  2794.     {
  2795.       XSETINT (end, st.st_size);
  2796.       if (XINT (end) != st.st_size)
  2797.     error ("maximum buffer size exceeded");
  2798.     }
  2799.  
  2800.   /* If requested, replace the accessible part of the buffer
  2801.      with the file contents.  Avoid replacing text at the
  2802.      beginning or end of the buffer that matches the file contents;
  2803.      that preserves markers pointing to the unchanged parts.  */
  2804. #ifdef MSDOS
  2805.   /* On MSDOS, replace mode doesn't really work, except for binary files,
  2806.      and it's not worth supporting just for them.  */
  2807.   if (!NILP (replace))
  2808.     {
  2809.       replace = Qnil;
  2810.       XFASTINT (beg) = 0;
  2811.       XFASTINT (end) = st.st_size;
  2812.       del_range_1 (BEGV, ZV, 0);
  2813.     }
  2814. #else /* MSDOS */
  2815.   if (!NILP (replace))
  2816.     {
  2817.       unsigned char buffer[1 << 14];
  2818.       int same_at_start = BEGV;
  2819.       int same_at_end = ZV;
  2820.       int overlap;
  2821.  
  2822.       immediate_quit = 1;
  2823.       QUIT;
  2824.       /* Count how many chars at the start of the file
  2825.      match the text at the beginning of the buffer.  */
  2826.       while (1)
  2827.     {
  2828.       int nread, bufpos;
  2829.  
  2830.       nread = read (fd, buffer, sizeof buffer);
  2831.       if (nread < 0)
  2832.         error ("IO error reading %s: %s",
  2833.            XSTRING (filename)->data, strerror (errno));
  2834.       else if (nread == 0)
  2835.         break;
  2836.       bufpos = 0;
  2837.       while (bufpos < nread && same_at_start < ZV
  2838.          && FETCH_CHAR (same_at_start) == buffer[bufpos])
  2839.         same_at_start++, bufpos++;
  2840.       /* If we found a discrepancy, stop the scan.
  2841.          Otherwise loop around and scan the next bufferfull.  */
  2842.       if (bufpos != nread)
  2843.         break;
  2844.     }
  2845.       immediate_quit = 0;
  2846.       /* If the file matches the buffer completely,
  2847.      there's no need to replace anything.  */
  2848.       if (same_at_start - BEGV == st.st_size)
  2849.     {
  2850.       close (fd);
  2851.       specpdl_ptr--;
  2852.       /* Truncate the buffer to the size of the file.  */
  2853.       del_range_1 (same_at_start, same_at_end, 0);
  2854.       goto handled;
  2855.     }
  2856.       immediate_quit = 1;
  2857.       QUIT;
  2858.       /* Count how many chars at the end of the file
  2859.      match the text at the end of the buffer.  */
  2860.       while (1)
  2861.     {
  2862.       int total_read, nread, bufpos, curpos, trial;
  2863.  
  2864.       /* At what file position are we now scanning?  */
  2865.       curpos = st.st_size - (ZV - same_at_end);
  2866.       /* If the entire file matches the buffer tail, stop the scan.  */
  2867.       if (curpos == 0)
  2868.         break;
  2869.       /* How much can we scan in the next step?  */
  2870.       trial = min (curpos, sizeof buffer);
  2871.       if (lseek (fd, curpos - trial, 0) < 0)
  2872.         report_file_error ("Setting file position",
  2873.                    Fcons (filename, Qnil));
  2874.  
  2875.       total_read = 0;
  2876.       while (total_read < trial)
  2877.         {
  2878.           nread = read (fd, buffer + total_read, trial - total_read);
  2879.           if (nread <= 0)
  2880.         error ("IO error reading %s: %s",
  2881.                XSTRING (filename)->data, strerror (errno));
  2882.           total_read += nread;
  2883.         }
  2884.       /* Scan this bufferfull from the end, comparing with
  2885.          the Emacs buffer.  */
  2886.       bufpos = total_read;
  2887.       /* Compare with same_at_start to avoid counting some buffer text
  2888.          as matching both at the file's beginning and at the end.  */
  2889.       while (bufpos > 0 && same_at_end > same_at_start
  2890.          && FETCH_CHAR (same_at_end - 1) == buffer[bufpos - 1])
  2891.         same_at_end--, bufpos--;
  2892.       /* If we found a discrepancy, stop the scan.
  2893.          Otherwise loop around and scan the preceding bufferfull.  */
  2894.       if (bufpos != 0)
  2895.         break;
  2896.     }
  2897.       immediate_quit = 0;
  2898.  
  2899.       /* Don't try to reuse the same piece of text twice.  */
  2900.       overlap = same_at_start - BEGV - (same_at_end + st.st_size - ZV);
  2901.       if (overlap > 0)
  2902.     same_at_end += overlap;
  2903.  
  2904.       /* Arrange to read only the nonmatching middle part of the file.  */
  2905.       XFASTINT (beg) = same_at_start - BEGV;
  2906.       XFASTINT (end) = st.st_size - (ZV - same_at_end);
  2907.  
  2908.       del_range_1 (same_at_start, same_at_end, 0);
  2909.       /* Insert from the file at the proper position.  */
  2910.       SET_PT (same_at_start);
  2911.     }
  2912. #endif /* MSDOS */
  2913.  
  2914.   total = XINT (end) - XINT (beg);
  2915.  
  2916.   {
  2917.     register Lisp_Object temp;
  2918.  
  2919.     /* Make sure point-max won't overflow after this insertion.  */
  2920.     XSET (temp, Lisp_Int, total);
  2921.     if (total != XINT (temp))
  2922.       error ("maximum buffer size exceeded");
  2923.   }
  2924.  
  2925.   if (NILP (visit) && total > 0)
  2926.     prepare_to_modify_buffer (point, point);
  2927.  
  2928.   move_gap (point);
  2929.   if (GAP_SIZE < total)
  2930.     make_gap (total - GAP_SIZE);
  2931.  
  2932.   if (XINT (beg) != 0 || !NILP (replace))
  2933.     {
  2934.       if (lseek (fd, XINT (beg), 0) < 0)
  2935.     report_file_error ("Setting file position", Fcons (filename, Qnil));
  2936.     }
  2937.  
  2938.   how_much = 0;
  2939.   while (inserted < total)
  2940.     {
  2941.       int try = min (total - inserted, 64 << 10);
  2942.       int this;
  2943.  
  2944.       /* Allow quitting out of the actual I/O.  */
  2945.       immediate_quit = 1;
  2946.       QUIT;
  2947.       this = read (fd, &FETCH_CHAR (point + inserted - 1) + 1, try);
  2948.       immediate_quit = 0;
  2949.  
  2950.       if (this <= 0)
  2951.     {
  2952.       how_much = this;
  2953.       break;
  2954.     }
  2955.  
  2956.       GPT += this;
  2957.       GAP_SIZE -= this;
  2958.       ZV += this;
  2959.       Z += this;
  2960.       inserted += this;
  2961.     }
  2962.  
  2963. #ifdef MSDOS
  2964.   /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
  2965.   /* Determine file type from name and remove LFs from CR-LFs if the file
  2966.      is deemed to be a text file.  */
  2967.   {
  2968.     struct gcpro gcpro1;
  2969.     Lisp_Object code;
  2970.     code = Qnil;
  2971.     GCPRO1 (filename);
  2972.     current_buffer->buffer_file_type
  2973.       = call1 (Qfind_buffer_file_type, filename);
  2974.     UNGCPRO;
  2975.     if (NILP (current_buffer->buffer_file_type))
  2976.       {
  2977.     int reduced_size
  2978.       = inserted - crlf_to_lf (inserted, &FETCH_CHAR (point - 1) + 1);
  2979.     ZV -= reduced_size;
  2980.     Z -= reduced_size;
  2981.     GPT -= reduced_size;
  2982.     GAP_SIZE += reduced_size;
  2983.     inserted -= reduced_size;
  2984.       }
  2985.   }
  2986. #endif
  2987.  
  2988.   if (inserted > 0)
  2989.     {
  2990.       record_insert (point, inserted);
  2991.  
  2992.       /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  2993.       offset_intervals (current_buffer, point, inserted);
  2994.       MODIFF++;
  2995.     }
  2996.  
  2997.   close (fd);
  2998.  
  2999.   /* Discard the unwind protect for closing the file.  */
  3000.   specpdl_ptr--;
  3001.  
  3002.   if (how_much < 0)
  3003.     error ("IO error reading %s: %s",
  3004.        XSTRING (filename)->data, strerror (errno));
  3005.  
  3006.  notfound:
  3007.  handled:
  3008.  
  3009.   if (!NILP (visit))
  3010.     {
  3011.       if (!EQ (current_buffer->undo_list, Qt))
  3012.     current_buffer->undo_list = Qnil;
  3013. #ifdef APOLLO
  3014.       stat (XSTRING (filename)->data, &st);
  3015. #endif
  3016.  
  3017.       if (NILP (handler))
  3018.     {
  3019.       current_buffer->modtime = st.st_mtime;
  3020.       current_buffer->filename = filename;
  3021.     }
  3022.  
  3023.       current_buffer->save_modified = MODIFF;
  3024.       current_buffer->auto_save_modified = MODIFF;
  3025.       XFASTINT (current_buffer->save_length) = Z - BEG;
  3026. #ifdef CLASH_DETECTION
  3027.       if (NILP (handler))
  3028.     {
  3029.       if (!NILP (current_buffer->filename))
  3030.         unlock_file (current_buffer->filename);
  3031.       unlock_file (filename);
  3032.     }
  3033. #endif /* CLASH_DETECTION */
  3034.       /* If visiting nonexistent file, return nil.  */
  3035.       if (current_buffer->modtime == -1)
  3036.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  3037.     }
  3038.  
  3039.   if (inserted > 0 && NILP (visit) && total > 0)
  3040.     signal_after_change (point, 0, inserted);
  3041.   
  3042.   if (inserted > 0)
  3043.     {
  3044.       p = Vafter_insert_file_functions;
  3045.       while (!NILP (p))
  3046.     {
  3047.       insval = call1 (Fcar (p), make_number (inserted));
  3048.       if (!NILP (insval))
  3049.         {
  3050.           CHECK_NUMBER (insval, 0);
  3051.           inserted = XFASTINT (insval);
  3052.         }
  3053.       QUIT;
  3054.       p = Fcdr (p);
  3055.     }
  3056.     }
  3057.  
  3058.   if (NILP (val))
  3059.     val = Fcons (filename,
  3060.          Fcons (make_number (inserted),
  3061.             Qnil));
  3062.  
  3063.   RETURN_UNGCPRO (unbind_to (count, val));
  3064. }
  3065.  
  3066. static Lisp_Object build_annotations ();
  3067.  
  3068. DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 5,
  3069.   "r\nFWrite region to file: ",
  3070.   "Write current region into specified file.\n\
  3071. When called from a program, takes three arguments:\n\
  3072. START, END and FILENAME.  START and END are buffer positions.\n\
  3073. Optional fourth argument APPEND if non-nil means\n\
  3074.   append to existing file contents (if any).\n\
  3075. Optional fifth argument VISIT if t means\n\
  3076.   set the last-save-file-modtime of buffer to this file's modtime\n\
  3077.   and mark buffer not modified.\n\
  3078. If VISIT is a string, it is a second file name;\n\
  3079.   the output goes to FILENAME, but the buffer is marked as visiting VISIT.\n\
  3080.   VISIT is also the file name to lock and unlock for clash detection.\n\
  3081. If VISIT is neither t nor nil nor a string,\n\
  3082.   that means do not print the \"Wrote file\" message.\n\
  3083. Kludgy feature: if START is a string, then that string is written\n\
  3084. to the file, instead of any buffer contents, and END is ignored.")
  3085.   (start, end, filename, append, visit)
  3086.      Lisp_Object start, end, filename, append, visit;
  3087. {
  3088.   register int desc;
  3089.   int failure;
  3090.   int save_errno;
  3091.   unsigned char *fn;
  3092.   struct stat st;
  3093.   int tem;
  3094.   int count = specpdl_ptr - specpdl;
  3095. #ifdef VMS
  3096.   unsigned char *fname = 0;    /* If non-0, original filename (must rename) */
  3097. #endif /* VMS */
  3098.   Lisp_Object handler;
  3099.   Lisp_Object visit_file;
  3100.   Lisp_Object annotations;
  3101.   int visiting, quietly;
  3102.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  3103. #ifdef MSDOS
  3104.   int buffer_file_type
  3105.     = NILP (current_buffer->buffer_file_type) ? O_TEXT : O_BINARY;
  3106. #endif
  3107.  
  3108.   if (!NILP (start) && !STRINGP (start))
  3109.     validate_region (&start, &end);
  3110.  
  3111.   filename = Fexpand_file_name (filename, Qnil);
  3112.   if (STRINGP (visit))
  3113.     visit_file = Fexpand_file_name (visit, Qnil);
  3114.   else
  3115.     visit_file = filename;
  3116.  
  3117.   visiting = (EQ (visit, Qt) || STRINGP (visit));
  3118.   quietly = !NILP (visit);
  3119.  
  3120.   annotations = Qnil;
  3121.  
  3122.   GCPRO4 (start, filename, annotations, visit_file);
  3123.  
  3124.   /* If the file name has special constructs in it,
  3125.      call the corresponding file handler.  */
  3126.   handler = Ffind_file_name_handler (filename, Qwrite_region);
  3127.   /* If FILENAME has no handler, see if VISIT has one.  */
  3128.   if (NILP (handler) && XTYPE (visit) == Lisp_String)
  3129.     handler = Ffind_file_name_handler (visit, Qwrite_region);    
  3130.  
  3131.   if (!NILP (handler))
  3132.     {
  3133.       Lisp_Object val;
  3134.       val = call6 (handler, Qwrite_region, start, end,
  3135.            filename, append, visit);
  3136.  
  3137.       if (visiting)
  3138.     {
  3139.       current_buffer->save_modified = MODIFF;
  3140.       XFASTINT (current_buffer->save_length) = Z - BEG;
  3141.       current_buffer->filename = visit_file;
  3142.     }
  3143.       UNGCPRO;
  3144.       return val;
  3145.     }
  3146.  
  3147.   /* Special kludge to simplify auto-saving.  */
  3148.   if (NILP (start))
  3149.     {
  3150.       XFASTINT (start) = BEG;
  3151.       XFASTINT (end) = Z;
  3152.     }
  3153.  
  3154.   annotations = build_annotations (start, end);
  3155.  
  3156. #ifdef CLASH_DETECTION
  3157.   if (!auto_saving)
  3158.     lock_file (visit_file);
  3159. #endif /* CLASH_DETECTION */
  3160.  
  3161.   fn = XSTRING (filename)->data;
  3162.   desc = -1;
  3163.   if (!NILP (append))
  3164. #ifdef MSDOS
  3165.     desc = open (fn, O_WRONLY | buffer_file_type);
  3166. #else
  3167.     desc = open (fn, O_WRONLY);
  3168. #endif
  3169.  
  3170.   if (desc < 0)
  3171. #ifdef VMS
  3172.     if (auto_saving)    /* Overwrite any previous version of autosave file */
  3173.       {
  3174.     vms_truncate (fn);    /* if fn exists, truncate to zero length */
  3175.     desc = open (fn, O_RDWR);
  3176.     if (desc < 0)
  3177.       desc = creat_copy_attrs (STRINGP (current_buffer->filename)
  3178.                    ? XSTRING (current_buffer->filename)->data : 0,
  3179.                    fn);
  3180.       }
  3181.     else        /* Write to temporary name and rename if no errors */
  3182.       {
  3183.     Lisp_Object temp_name;
  3184.     temp_name = Ffile_name_directory (filename);
  3185.  
  3186.     if (!NILP (temp_name))
  3187.       {
  3188.         temp_name = Fmake_temp_name (concat2 (temp_name,
  3189.                           build_string ("$$SAVE$$")));
  3190.         fname = XSTRING (filename)->data;
  3191.         fn = XSTRING (temp_name)->data;
  3192.         desc = creat_copy_attrs (fname, fn);
  3193.         if (desc < 0)
  3194.           {
  3195.         /* If we can't open the temporary file, try creating a new
  3196.            version of the original file.  VMS "creat" creates a
  3197.            new version rather than truncating an existing file. */
  3198.         fn = fname;
  3199.         fname = 0;
  3200.         desc = creat (fn, 0666);
  3201. #if 0 /* This can clobber an existing file and fail to replace it,
  3202.      if the user runs out of space.  */
  3203.         if (desc < 0)
  3204.           {
  3205.             /* We can't make a new version;
  3206.                try to truncate and rewrite existing version if any.  */
  3207.             vms_truncate (fn);
  3208.             desc = open (fn, O_RDWR);
  3209.           }
  3210. #endif
  3211.           }
  3212.       }
  3213.     else
  3214.       desc = creat (fn, 0666);
  3215.       }
  3216. #else /* not VMS */
  3217. #ifdef MSDOS
  3218.   desc = open (fn, 
  3219.            O_WRONLY | O_TRUNC | O_CREAT | buffer_file_type, 
  3220.            S_IREAD | S_IWRITE);
  3221. #else /* not MSDOS */
  3222.   desc = creat (fn, auto_saving ? auto_save_mode_bits : 0666);
  3223. #endif /* not MSDOS */
  3224. #endif /* not VMS */
  3225.  
  3226.   UNGCPRO;
  3227.  
  3228.   if (desc < 0)
  3229.     {
  3230. #ifdef CLASH_DETECTION
  3231.       save_errno = errno;
  3232.       if (!auto_saving) unlock_file (visit_file);
  3233.       errno = save_errno;
  3234. #endif /* CLASH_DETECTION */
  3235.       report_file_error ("Opening output file", Fcons (filename, Qnil));
  3236.     }
  3237.  
  3238.   record_unwind_protect (close_file_unwind, make_number (desc));
  3239.  
  3240.   if (!NILP (append))
  3241.     if (lseek (desc, 0, 2) < 0)
  3242.       {
  3243. #ifdef CLASH_DETECTION
  3244.     if (!auto_saving) unlock_file (visit_file);
  3245. #endif /* CLASH_DETECTION */
  3246.     report_file_error ("Lseek error", Fcons (filename, Qnil));
  3247.       }
  3248.  
  3249. #ifdef VMS
  3250. /*
  3251.  * Kludge Warning: The VMS C RTL likes to insert carriage returns
  3252.  * if we do writes that don't end with a carriage return. Furthermore
  3253.  * it cannot handle writes of more then 16K. The modified
  3254.  * version of "sys_write" in SYSDEP.C (see comment there) copes with
  3255.  * this EXCEPT for the last record (iff it doesn't end with a carriage
  3256.  * return). This implies that if your buffer doesn't end with a carriage
  3257.  * return, you get one free... tough. However it also means that if
  3258.  * we make two calls to sys_write (a la the following code) you can
  3259.  * get one at the gap as well. The easiest way to fix this (honest)
  3260.  * is to move the gap to the next newline (or the end of the buffer).
  3261.  * Thus this change.
  3262.  *
  3263.  * Yech!
  3264.  */
  3265.   if (GPT > BEG && GPT_ADDR[-1] != '\n')
  3266.     move_gap (find_next_newline (GPT, 1));
  3267. #endif
  3268.  
  3269.   failure = 0;
  3270.   immediate_quit = 1;
  3271.  
  3272.   if (STRINGP (start))
  3273.     {
  3274.       failure = 0 > a_write (desc, XSTRING (start)->data,
  3275.                  XSTRING (start)->size, 0, &annotations);
  3276.       save_errno = errno;
  3277.     }
  3278.   else if (XINT (start) != XINT (end))
  3279.     {
  3280.       int nwritten = 0;
  3281.       if (XINT (start) < GPT)
  3282.     {
  3283.       register int end1 = XINT (end);
  3284.       tem = XINT (start);
  3285.       failure = 0 > a_write (desc, &FETCH_CHAR (tem),
  3286.                  min (GPT, end1) - tem, tem, &annotations);
  3287.       nwritten += min (GPT, end1) - tem;
  3288.       save_errno = errno;
  3289.     }
  3290.  
  3291.       if (XINT (end) > GPT && !failure)
  3292.     {
  3293.       tem = XINT (start);
  3294.       tem = max (tem, GPT);
  3295.       failure = 0 > a_write (desc, &FETCH_CHAR (tem), XINT (end) - tem,
  3296.                  tem, &annotations);
  3297.       nwritten += XINT (end) - tem;
  3298.       save_errno = errno;
  3299.     }
  3300.  
  3301.       if (nwritten == 0)
  3302.     {
  3303.       /* If file was empty, still need to write the annotations */
  3304.       failure = 0 > a_write (desc, "", 0, XINT (start), &annotations);
  3305.       save_errno = errno;
  3306.     }
  3307.     }
  3308.  
  3309.   immediate_quit = 0;
  3310.  
  3311. #ifdef HAVE_FSYNC
  3312.   /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
  3313.      Disk full in NFS may be reported here.  */
  3314.   /* mib says that closing the file will try to write as fast as NFS can do
  3315.      it, and that means the fsync here is not crucial for autosave files.  */
  3316.   if (!auto_saving && fsync (desc) < 0)
  3317.     failure = 1, save_errno = errno;
  3318. #endif
  3319.  
  3320.   /* Spurious "file has changed on disk" warnings have been 
  3321.      observed on Suns as well.
  3322.      It seems that `close' can change the modtime, under nfs.
  3323.  
  3324.      (This has supposedly been fixed in Sunos 4,
  3325.      but who knows about all the other machines with NFS?)  */
  3326. #if 0
  3327.  
  3328.   /* On VMS and APOLLO, must do the stat after the close
  3329.      since closing changes the modtime.  */
  3330. #ifndef VMS
  3331. #ifndef APOLLO
  3332.   /* Recall that #if defined does not work on VMS.  */
  3333. #define FOO
  3334.   fstat (desc, &st);
  3335. #endif
  3336. #endif
  3337. #endif
  3338.  
  3339.   /* NFS can report a write failure now.  */
  3340.   if (close (desc) < 0)
  3341.     failure = 1, save_errno = errno;
  3342.  
  3343. #ifdef VMS
  3344.   /* If we wrote to a temporary name and had no errors, rename to real name. */
  3345.   if (fname)
  3346.     {
  3347.       if (!failure)
  3348.     failure = (rename (fn, fname) != 0), save_errno = errno;
  3349.       fn = fname;
  3350.     }
  3351. #endif /* VMS */
  3352.  
  3353. #ifndef FOO
  3354.   stat (fn, &st);
  3355. #endif
  3356.   /* Discard the unwind protect */
  3357.   specpdl_ptr = specpdl + count;
  3358.  
  3359. #ifdef CLASH_DETECTION
  3360.   if (!auto_saving)
  3361.     unlock_file (visit_file);
  3362. #endif /* CLASH_DETECTION */
  3363.  
  3364.   /* Do this before reporting IO error
  3365.      to avoid a "file has changed on disk" warning on
  3366.      next attempt to save.  */
  3367.   if (visiting)
  3368.     current_buffer->modtime = st.st_mtime;
  3369.  
  3370.   if (failure)
  3371.     error ("IO error writing %s: %s", fn, strerror (save_errno));
  3372.  
  3373.   if (visiting)
  3374.     {
  3375.       current_buffer->save_modified = MODIFF;
  3376.       XFASTINT (current_buffer->save_length) = Z - BEG;
  3377.       current_buffer->filename = visit_file;
  3378.       update_mode_lines++;
  3379.     }
  3380.   else if (quietly)
  3381.     return Qnil;
  3382.  
  3383.   if (!auto_saving)
  3384.     message ("Wrote %s", XSTRING (visit_file)->data);
  3385.  
  3386.   return Qnil;
  3387. }
  3388.  
  3389. Lisp_Object merge ();
  3390.  
  3391. DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0,
  3392.   "Return t if (car A) is numerically less than (car B).")
  3393.   (a, b)
  3394.      Lisp_Object a, b;
  3395. {
  3396.   return Flss (Fcar (a), Fcar (b));
  3397. }
  3398.  
  3399. /* Build the complete list of annotations appropriate for writing out
  3400.    the text between START and END, by calling all the functions in
  3401.    write-region-annotate-functions and merging the lists they return.  */
  3402.  
  3403. static Lisp_Object
  3404. build_annotations (start, end)
  3405.      Lisp_Object start, end;
  3406. {
  3407.   Lisp_Object annotations;
  3408.   Lisp_Object p, res;
  3409.   struct gcpro gcpro1, gcpro2;
  3410.  
  3411.   annotations = Qnil;
  3412.   p = Vwrite_region_annotate_functions;
  3413.   GCPRO2 (annotations, p);
  3414.   while (!NILP (p))
  3415.     {
  3416.       res = call2 (Fcar (p), start, end);
  3417.       Flength (res);   /* Check basic validity of return value */
  3418.       annotations = merge (annotations, res, Qcar_less_than_car);
  3419.       p = Fcdr (p);
  3420.     }
  3421.   UNGCPRO;
  3422.   return annotations;
  3423. }
  3424.  
  3425. /* Write to descriptor DESC the LEN characters starting at ADDR,
  3426.    assuming they start at position POS in the buffer.
  3427.    Intersperse with them the annotations from *ANNOT
  3428.    (those which fall within the range of positions POS to POS + LEN),
  3429.    each at its appropriate position.
  3430.  
  3431.    Modify *ANNOT by discarding elements as we output them.
  3432.    The return value is negative in case of system call failure.  */
  3433.  
  3434. int
  3435. a_write (desc, addr, len, pos, annot)
  3436.      int desc;
  3437.      register char *addr;
  3438.      register int len;
  3439.      int pos;
  3440.      Lisp_Object *annot;
  3441. {
  3442.   Lisp_Object tem;
  3443.   int nextpos;
  3444.   int lastpos = pos + len;
  3445.  
  3446.   while (1)
  3447.     {
  3448.       tem = Fcar_safe (Fcar (*annot));
  3449.       if (INTEGERP (tem) && XINT (tem) >= pos && XFASTINT (tem) <= lastpos)
  3450.     nextpos = XFASTINT (tem);
  3451.       else
  3452.     return e_write (desc, addr, lastpos - pos);
  3453.       if (nextpos > pos)
  3454.     {
  3455.       if (0 > e_write (desc, addr, nextpos - pos))
  3456.         return -1;
  3457.       addr += nextpos - pos;
  3458.       pos = nextpos;
  3459.     }
  3460.       tem = Fcdr (Fcar (*annot));
  3461.       if (STRINGP (tem))
  3462.     {
  3463.       if (0 > e_write (desc, XSTRING (tem)->data, XSTRING (tem)->size))
  3464.         return -1;
  3465.     }
  3466.       *annot = Fcdr (*annot);
  3467.     }
  3468. }
  3469.  
  3470. int
  3471. e_write (desc, addr, len)
  3472.      int desc;
  3473.      register char *addr;
  3474.      register int len;
  3475. {
  3476.   char buf[16 * 1024];
  3477.   register char *p, *end;
  3478.  
  3479.   if (!EQ (current_buffer->selective_display, Qt))
  3480.     return write (desc, addr, len) - len;
  3481.   else
  3482.     {
  3483.       p = buf;
  3484.       end = p + sizeof buf;
  3485.       while (len--)
  3486.     {
  3487.       if (p == end)
  3488.         {
  3489.           if (write (desc, buf, sizeof buf) != sizeof buf)
  3490.         return -1;
  3491.           p = buf;
  3492.         }
  3493.       *p = *addr++;
  3494.       if (*p++ == '\015')
  3495.         p[-1] = '\n';
  3496.     }
  3497.       if (p != buf)
  3498.     if (write (desc, buf, p - buf) != p - buf)
  3499.       return -1;
  3500.     }
  3501.   return 0;
  3502. }
  3503.  
  3504. DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime,
  3505.   Sverify_visited_file_modtime, 1, 1, 0,
  3506.   "Return t if last mod time of BUF's visited file matches what BUF records.\n\
  3507. This means that the file has not been changed since it was visited or saved.")
  3508.   (buf)
  3509.      Lisp_Object buf;
  3510. {
  3511.   struct buffer *b;
  3512.   struct stat st;
  3513.   Lisp_Object handler;
  3514.  
  3515.   CHECK_BUFFER (buf, 0);
  3516.   b = XBUFFER (buf);
  3517.  
  3518.   if (XTYPE (b->filename) != Lisp_String) return Qt;
  3519.   if (b->modtime == 0) return Qt;
  3520.  
  3521.   /* If the file name has special constructs in it,
  3522.      call the corresponding file handler.  */
  3523.   handler = Ffind_file_name_handler (b->filename,
  3524.                      Qverify_visited_file_modtime);
  3525.   if (!NILP (handler))
  3526.     return call2 (handler, Qverify_visited_file_modtime, buf);
  3527.  
  3528.   if (stat (XSTRING (b->filename)->data, &st) < 0)
  3529.     {
  3530.       /* If the file doesn't exist now and didn't exist before,
  3531.      we say that it isn't modified, provided the error is a tame one.  */
  3532.       if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
  3533.     st.st_mtime = -1;
  3534.       else
  3535.     st.st_mtime = 0;
  3536.     }
  3537.   if (st.st_mtime == b->modtime
  3538.       /* If both are positive, accept them if they are off by one second.  */
  3539.       || (st.st_mtime > 0 && b->modtime > 0
  3540.       && (st.st_mtime == b->modtime + 1
  3541.           || st.st_mtime == b->modtime - 1)))
  3542.     return Qt;
  3543.   return Qnil;
  3544. }
  3545.  
  3546. DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime,
  3547.   Sclear_visited_file_modtime, 0, 0, 0,
  3548.   "Clear out records of last mod time of visited file.\n\
  3549. Next attempt to save will certainly not complain of a discrepancy.")
  3550.   ()
  3551. {
  3552.   current_buffer->modtime = 0;
  3553.   return Qnil;
  3554. }
  3555.  
  3556. DEFUN ("visited-file-modtime", Fvisited_file_modtime,
  3557.   Svisited_file_modtime, 0, 0, 0,
  3558.   "Return the current buffer's recorded visited file modification time.\n\
  3559. The value is a list of the form (HIGH . LOW), like the time values\n\
  3560. that `file-attributes' returns.")
  3561.   ()
  3562. {
  3563.   return long_to_cons (current_buffer->modtime);
  3564. }
  3565.  
  3566. DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime,
  3567.   Sset_visited_file_modtime, 0, 1, 0,
  3568.   "Update buffer's recorded modification time from the visited file's time.\n\
  3569. Useful if the buffer was not read from the file normally\n\
  3570. or if the file itself has been changed for some known benign reason.\n\
  3571. An argument specifies the modification time value to use\n\
  3572. \(instead of that of the visited file), in the form of a list\n\
  3573. \(HIGH . LOW) or (HIGH LOW).")
  3574.   (time_list)
  3575.      Lisp_Object time_list;
  3576. {
  3577.   if (!NILP (time_list))
  3578.     current_buffer->modtime = cons_to_long (time_list);
  3579.   else
  3580.     {
  3581.       register Lisp_Object filename;
  3582.       struct stat st;
  3583.       Lisp_Object handler;
  3584.  
  3585.       filename = Fexpand_file_name (current_buffer->filename, Qnil);
  3586.  
  3587.       /* If the file name has special constructs in it,
  3588.      call the corresponding file handler.  */
  3589.       handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime);
  3590.       if (!NILP (handler))
  3591.     /* The handler can find the file name the same way we did.  */
  3592.     return call2 (handler, Qset_visited_file_modtime, Qnil);
  3593.       else if (stat (XSTRING (filename)->data, &st) >= 0)
  3594.     current_buffer->modtime = st.st_mtime;
  3595.     }
  3596.  
  3597.   return Qnil;
  3598. }
  3599.  
  3600. Lisp_Object
  3601. auto_save_error ()
  3602. {
  3603.   unsigned char *name = XSTRING (current_buffer->name)->data;
  3604.  
  3605.   ring_bell ();
  3606.   message ("Autosaving...error for %s", name);
  3607.   Fsleep_for (make_number (1), Qnil);
  3608.   message ("Autosaving...error!for %s", name);
  3609.   Fsleep_for (make_number (1), Qnil);
  3610.   message ("Autosaving...error for %s", name);
  3611.   Fsleep_for (make_number (1), Qnil);
  3612.   return Qnil;
  3613. }
  3614.  
  3615. Lisp_Object
  3616. auto_save_1 ()
  3617. {
  3618.   unsigned char *fn;
  3619.   struct stat st;
  3620.  
  3621.   /* Get visited file's mode to become the auto save file's mode.  */
  3622.   if (stat (XSTRING (current_buffer->filename)->data, &st) >= 0)
  3623.     /* But make sure we can overwrite it later!  */
  3624.     auto_save_mode_bits = st.st_mode | 0600;
  3625.   else
  3626.     auto_save_mode_bits = 0666;
  3627.  
  3628.   return
  3629.     Fwrite_region (Qnil, Qnil,
  3630.            current_buffer->auto_save_file_name,
  3631.            Qnil, Qlambda);
  3632. }
  3633.  
  3634. static Lisp_Object
  3635. do_auto_save_unwind (desc)  /* used as unwind-protect function */
  3636.      Lisp_Object desc;
  3637. {
  3638.   close (XINT (desc));
  3639.   return Qnil;
  3640. }
  3641.  
  3642. DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "",
  3643.   "Auto-save all buffers that need it.\n\
  3644. This is all buffers that have auto-saving enabled\n\
  3645. and are changed since last auto-saved.\n\
  3646. Auto-saving writes the buffer into a file\n\
  3647. so that your editing is not lost if the system crashes.\n\
  3648. This file is not the file you visited; that changes only when you save.\n\
  3649. Normally we run the normal hook `auto-save-hook' before saving.\n\n\
  3650. Non-nil first argument means do not print any message if successful.\n\
  3651. Non-nil second argument means save only current buffer.")
  3652.   (no_message, current_only)
  3653.      Lisp_Object no_message, current_only;
  3654. {
  3655.   struct buffer *old = current_buffer, *b;
  3656.   Lisp_Object tail, buf;
  3657.   int auto_saved = 0;
  3658.   char *omessage = echo_area_glyphs;
  3659.   int omessage_length = echo_area_glyphs_length;
  3660.   extern int minibuf_level;
  3661.   int do_handled_files;
  3662.   Lisp_Object oquit;
  3663.   int listdesc;
  3664.   Lisp_Object lispstream;
  3665.   int count = specpdl_ptr - specpdl;
  3666.   int *ptr;
  3667.  
  3668.   /* Ordinarily don't quit within this function,
  3669.      but don't make it impossible to quit (in case we get hung in I/O).  */
  3670.   oquit = Vquit_flag;
  3671.   Vquit_flag = Qnil;
  3672.  
  3673.   /* No GCPRO needed, because (when it matters) all Lisp_Object variables
  3674.      point to non-strings reached from Vbuffer_alist.  */
  3675.  
  3676.   auto_saving = 1;
  3677.   if (minibuf_level)
  3678.     no_message = Qt;
  3679.  
  3680.   if (!NILP (Vrun_hooks))
  3681.     call1 (Vrun_hooks, intern ("auto-save-hook"));
  3682.  
  3683.   if (STRINGP (Vauto_save_list_file_name))
  3684.     {
  3685. #ifdef MSDOS
  3686.       listdesc = open (XSTRING (Vauto_save_list_file_name)->data, 
  3687.                O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
  3688.                S_IREAD | S_IWRITE);
  3689. #else /* not MSDOS */
  3690.       listdesc = creat (XSTRING (Vauto_save_list_file_name)->data, 0666);
  3691. #endif /* not MSDOS */
  3692.     }
  3693.   else
  3694.     listdesc = -1;
  3695.   
  3696.   /* Arrange to close that file whether or not we get an error.  */
  3697.   if (listdesc >= 0)
  3698.     record_unwind_protect (do_auto_save_unwind, make_number (listdesc));
  3699.  
  3700.   /* First, save all files which don't have handlers.  If Emacs is
  3701.      crashing, the handlers may tweak what is causing Emacs to crash
  3702.      in the first place, and it would be a shame if Emacs failed to
  3703.      autosave perfectly ordinary files because it couldn't handle some
  3704.      ange-ftp'd file.  */
  3705.   for (do_handled_files = 0; do_handled_files < 2; do_handled_files++)
  3706.     for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  3707.      tail = XCONS (tail)->cdr)
  3708.       {
  3709.     buf = XCONS (XCONS (tail)->car)->cdr;
  3710.     b = XBUFFER (buf);
  3711.       
  3712.     /* Record all the buffers that have auto save mode
  3713.        in the special file that lists them.  */
  3714.     if (XTYPE (b->auto_save_file_name) == Lisp_String
  3715.         && listdesc >= 0 && do_handled_files == 0)
  3716.       {
  3717.         write (listdesc, XSTRING (b->auto_save_file_name)->data,
  3718.            XSTRING (b->auto_save_file_name)->size);
  3719.         write (listdesc, "\n", 1);
  3720.       }
  3721.  
  3722.     if (!NILP (current_only)
  3723.         && b != current_buffer)
  3724.       continue;
  3725.  
  3726.     /* Check for auto save enabled
  3727.        and file changed since last auto save
  3728.        and file changed since last real save.  */
  3729.     if (XTYPE (b->auto_save_file_name) == Lisp_String
  3730.         && b->save_modified < BUF_MODIFF (b)
  3731.         && b->auto_save_modified < BUF_MODIFF (b)
  3732.         /* -1 means we've turned off autosaving for a while--see below.  */
  3733.         && XINT (b->save_length) >= 0
  3734.         && (do_handled_files
  3735.         || NILP (Ffind_file_name_handler (b->auto_save_file_name,
  3736.                           Qwrite_region))))
  3737.       {
  3738.         EMACS_TIME before_time, after_time;
  3739.  
  3740.         EMACS_GET_TIME (before_time);
  3741.  
  3742.         /* If we had a failure, don't try again for 20 minutes.  */
  3743.         if (b->auto_save_failure_time >= 0
  3744.         && EMACS_SECS (before_time) - b->auto_save_failure_time < 1200)
  3745.           continue;
  3746.  
  3747.         if ((XFASTINT (b->save_length) * 10
  3748.          > (BUF_Z (b) - BUF_BEG (b)) * 13)
  3749.         /* A short file is likely to change a large fraction;
  3750.            spare the user annoying messages.  */
  3751.         && XFASTINT (b->save_length) > 5000
  3752.         /* These messages are frequent and annoying for `*mail*'.  */
  3753.         && !EQ (b->filename, Qnil)
  3754.         && NILP (no_message))
  3755.           {
  3756.         /* It has shrunk too much; turn off auto-saving here.  */
  3757.         message ("Buffer %s has shrunk a lot; auto save turned off there",
  3758.              XSTRING (b->name)->data);
  3759.         /* Turn off auto-saving until there's a real save,
  3760.            and prevent any more warnings.  */
  3761.         XSET (b->save_length, Lisp_Int, -1);
  3762.         Fsleep_for (make_number (1), Qnil);
  3763.         continue;
  3764.           }
  3765.         set_buffer_internal (b);
  3766.         if (!auto_saved && NILP (no_message))
  3767.           message1 ("Auto-saving...");
  3768.         internal_condition_case (auto_save_1, Qt, auto_save_error);
  3769.         auto_saved++;
  3770.         b->auto_save_modified = BUF_MODIFF (b);
  3771.         XFASTINT (current_buffer->save_length) = Z - BEG;
  3772.         set_buffer_internal (old);
  3773.  
  3774.         EMACS_GET_TIME (after_time);
  3775.  
  3776.         /* If auto-save took more than 60 seconds,
  3777.            assume it was an NFS failure that got a timeout.  */
  3778.         if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60)
  3779.           b->auto_save_failure_time = EMACS_SECS (after_time);
  3780.       }
  3781.       }
  3782.  
  3783.   /* Prevent another auto save till enough input events come in.  */
  3784.   record_auto_save ();
  3785.  
  3786.   if (auto_saved && NILP (no_message))
  3787.     {
  3788.       if (omessage)
  3789.     message2 (omessage, omessage_length);
  3790.       else
  3791.     message1 ("Auto-saving...done");
  3792.     }
  3793.  
  3794.   Vquit_flag = oquit;
  3795.  
  3796.   auto_saving = 0;
  3797.   unbind_to (count, Qnil);
  3798.   return Qnil;
  3799. }
  3800.  
  3801. DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved,
  3802.   Sset_buffer_auto_saved, 0, 0, 0,
  3803.   "Mark current buffer as auto-saved with its current text.\n\
  3804. No auto-save file will be written until the buffer changes again.")
  3805.   ()
  3806. {
  3807.   current_buffer->auto_save_modified = MODIFF;
  3808.   XFASTINT (current_buffer->save_length) = Z - BEG;
  3809.   current_buffer->auto_save_failure_time = -1;
  3810.   return Qnil;
  3811. }
  3812.  
  3813. DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure,
  3814.   Sclear_buffer_auto_save_failure, 0, 0, 0,
  3815.   "Clear any record of a recent auto-save failure in the current buffer.")
  3816.   ()
  3817. {
  3818.   current_buffer->auto_save_failure_time = -1;
  3819.   return Qnil;
  3820. }
  3821.  
  3822. DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p,
  3823.   0, 0, 0,
  3824.   "Return t if buffer has been auto-saved since last read in or saved.")
  3825.   ()
  3826. {
  3827.   return (current_buffer->save_modified < current_buffer->auto_save_modified) ? Qt : Qnil;
  3828. }
  3829.  
  3830. /* Reading and completing file names */
  3831. extern Lisp_Object Ffile_name_completion (), Ffile_name_all_completions ();
  3832.  
  3833. /* In the string VAL, change each $ to $$ and return the result.  */
  3834.  
  3835. static Lisp_Object
  3836. double_dollars (val)
  3837.      Lisp_Object val;
  3838. {
  3839.   register unsigned char *old, *new;
  3840.   register int n;
  3841.   int osize, count;
  3842.  
  3843.   osize = XSTRING (val)->size;
  3844.   /* Quote "$" as "$$" to get it past substitute-in-file-name */
  3845.   for (n = osize, count = 0, old = XSTRING (val)->data; n > 0; n--)
  3846.     if (*old++ == '$') count++;
  3847.   if (count > 0)
  3848.     {
  3849.       old = XSTRING (val)->data;
  3850.       val = Fmake_string (make_number (osize + count), make_number (0));
  3851.       new = XSTRING (val)->data;
  3852.       for (n = osize; n > 0; n--)
  3853.     if (*old != '$')
  3854.       *new++ = *old++;
  3855.     else
  3856.       {
  3857.         *new++ = '$';
  3858.         *new++ = '$';
  3859.         old++;
  3860.       }
  3861.     }
  3862.   return val;
  3863. }
  3864.  
  3865. DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal,
  3866.   3, 3, 0,
  3867.   "Internal subroutine for read-file-name.  Do not call this.")
  3868.   (string, dir, action)
  3869.      Lisp_Object string, dir, action;
  3870.   /* action is nil for complete, t for return list of completions,
  3871.      lambda for verify final value */
  3872. {
  3873.   Lisp_Object name, specdir, realdir, val, orig_string;
  3874.   int changed;
  3875.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  3876.  
  3877.   realdir = dir;
  3878.   name = string;
  3879.   orig_string = Qnil;
  3880.   specdir = Qnil;
  3881.   changed = 0;
  3882.   /* No need to protect ACTION--we only compare it with t and nil.  */
  3883.   GCPRO4 (string, realdir, name, specdir);
  3884.  
  3885.   if (XSTRING (string)->size == 0)
  3886.     {
  3887.       if (EQ (action, Qlambda))
  3888.     {
  3889.       UNGCPRO;
  3890.       return Qnil;
  3891.     }
  3892.     }
  3893.   else
  3894.     {
  3895.       orig_string = string;
  3896.       string = Fsubstitute_in_file_name (string);
  3897.       changed = NILP (Fstring_equal (string, orig_string));
  3898.       name = Ffile_name_nondirectory (string);
  3899.       val = Ffile_name_directory (string);
  3900.       if (! NILP (val))
  3901.     realdir = Fexpand_file_name (val, realdir);
  3902.     }
  3903.  
  3904.   if (NILP (action))
  3905.     {
  3906.       specdir = Ffile_name_directory (string);
  3907.       val = Ffile_name_completion (name, realdir);
  3908.       UNGCPRO;
  3909.       if (XTYPE (val) != Lisp_String)
  3910.     {
  3911.       if (changed)
  3912.         return string;
  3913.       return val;
  3914.     }
  3915.  
  3916.       if (!NILP (specdir))
  3917.     val = concat2 (specdir, val);
  3918. #ifndef VMS
  3919.       return double_dollars (val);
  3920. #else /* not VMS */
  3921.       return val;
  3922. #endif /* not VMS */
  3923.     }
  3924.   UNGCPRO;
  3925.  
  3926.   if (EQ (action, Qt))
  3927.     return Ffile_name_all_completions (name, realdir);
  3928.   /* Only other case actually used is ACTION = lambda */
  3929. #ifdef VMS
  3930.   /* Supposedly this helps commands such as `cd' that read directory names,
  3931.      but can someone explain how it helps them? -- RMS */
  3932.   if (XSTRING (name)->size == 0)
  3933.     return Qt;
  3934. #endif /* VMS */
  3935.   return Ffile_exists_p (string);
  3936. }
  3937.  
  3938. DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 5, 0,
  3939.   "Read file name, prompting with PROMPT and completing in directory DIR.\n\
  3940. Value is not expanded---you must call `expand-file-name' yourself.\n\
  3941. Default name to DEFAULT if user enters a null string.\n\
  3942.  (If DEFAULT is omitted, the visited file name is used.)\n\
  3943. Fourth arg MUSTMATCH non-nil means require existing file's name.\n\
  3944.  Non-nil and non-t means also require confirmation after completion.\n\
  3945. Fifth arg INITIAL specifies text to start with.\n\
  3946. DIR defaults to current buffer's directory default.")
  3947.   (prompt, dir, defalt, mustmatch, initial)
  3948.      Lisp_Object prompt, dir, defalt, mustmatch, initial;
  3949. {
  3950.   Lisp_Object val, insdef, insdef1, tem;
  3951.   struct gcpro gcpro1, gcpro2;
  3952.   register char *homedir;
  3953.   int count;
  3954.  
  3955.   if (NILP (dir))
  3956.     dir = current_buffer->directory;
  3957.   if (NILP (defalt))
  3958.     defalt = current_buffer->filename;
  3959.  
  3960.   /* If dir starts with user's homedir, change that to ~. */
  3961.   homedir = (char *) egetenv ("HOME");
  3962.   if (homedir != 0
  3963.       && XTYPE (dir) == Lisp_String
  3964.       && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
  3965.       && XSTRING (dir)->data[strlen (homedir)] == '/')
  3966.     {
  3967.       dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
  3968.              XSTRING (dir)->size - strlen (homedir) + 1);
  3969.       XSTRING (dir)->data[0] = '~';
  3970.     }
  3971.  
  3972.   if (insert_default_directory)
  3973.     {
  3974.       insdef = dir;
  3975.       if (!NILP (initial))
  3976.     {
  3977.       Lisp_Object args[2], pos;
  3978.  
  3979.       args[0] = insdef;
  3980.       args[1] = initial;
  3981.       insdef = Fconcat (2, args);
  3982.       pos = make_number (XSTRING (double_dollars (dir))->size);
  3983.       insdef1 = Fcons (double_dollars (insdef), pos);
  3984.     }
  3985.       else
  3986.     insdef1 = double_dollars (insdef);
  3987.     }
  3988.   else if (!NILP (initial))
  3989.     {
  3990.       insdef = initial;
  3991.       insdef1 = Fcons (double_dollars (insdef), 0);
  3992.     }
  3993.   else
  3994.     insdef = Qnil, insdef1 = Qnil;
  3995.  
  3996. #ifdef VMS
  3997.   count = specpdl_ptr - specpdl;
  3998.   specbind (intern ("completion-ignore-case"), Qt);
  3999. #endif
  4000.  
  4001.   GCPRO2 (insdef, defalt);
  4002.   val = Fcompleting_read (prompt, intern ("read-file-name-internal"),
  4003.               dir, mustmatch, insdef1,
  4004.               Qfile_name_history);
  4005.  
  4006. #ifdef VMS
  4007.   unbind_to (count, Qnil);
  4008. #endif
  4009.  
  4010.   UNGCPRO;
  4011.   if (NILP (val))
  4012.     error ("No file name specified");
  4013.   tem = Fstring_equal (val, insdef);
  4014.   if (!NILP (tem) && !NILP (defalt))
  4015.     return defalt;
  4016.   if (XSTRING (val)->size == 0 && NILP (insdef))
  4017.     {
  4018.       if (!NILP (defalt))
  4019.     return defalt;
  4020.       else
  4021.     error ("No default file name");
  4022.     }
  4023.   return Fsubstitute_in_file_name (val);
  4024. }
  4025.  
  4026. #if 0                /* Old version */
  4027. DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 5, 0,
  4028.   /* Don't confuse make-docfile by having two doc strings for this function.
  4029.      make-docfile does not pay attention to #if, for good reason!  */
  4030.   0)
  4031.   (prompt, dir, defalt, mustmatch, initial)
  4032.      Lisp_Object prompt, dir, defalt, mustmatch, initial;
  4033. {
  4034.   Lisp_Object val, insdef, tem;
  4035.   struct gcpro gcpro1, gcpro2;
  4036.   register char *homedir;
  4037.   int count;
  4038.  
  4039.   if (NILP (dir))
  4040.     dir = current_buffer->directory;
  4041.   if (NILP (defalt))
  4042.     defalt = current_buffer->filename;
  4043.  
  4044.   /* If dir starts with user's homedir, change that to ~. */
  4045.   homedir = (char *) egetenv ("HOME");
  4046.   if (homedir != 0
  4047.       && XTYPE (dir) == Lisp_String
  4048.       && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
  4049.       && XSTRING (dir)->data[strlen (homedir)] == '/')
  4050.     {
  4051.       dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
  4052.              XSTRING (dir)->size - strlen (homedir) + 1);
  4053.       XSTRING (dir)->data[0] = '~';
  4054.     }
  4055.  
  4056.   if (!NILP (initial))
  4057.     insdef = initial;
  4058.   else if (insert_default_directory)
  4059.     insdef = dir;
  4060.   else
  4061.     insdef = build_string ("");
  4062.  
  4063. #ifdef VMS
  4064.   count = specpdl_ptr - specpdl;
  4065.   specbind (intern ("completion-ignore-case"), Qt);
  4066. #endif
  4067.  
  4068.   GCPRO2 (insdef, defalt);
  4069.   val = Fcompleting_read (prompt, intern ("read-file-name-internal"),
  4070.               dir, mustmatch,
  4071.               insert_default_directory ? insdef : Qnil,
  4072.               Qfile_name_history);
  4073.  
  4074. #ifdef VMS
  4075.   unbind_to (count, Qnil);
  4076. #endif
  4077.  
  4078.   UNGCPRO;
  4079.   if (NILP (val))
  4080.     error ("No file name specified");
  4081.   tem = Fstring_equal (val, insdef);
  4082.   if (!NILP (tem) && !NILP (defalt))
  4083.     return defalt;
  4084.   return Fsubstitute_in_file_name (val);
  4085. }
  4086. #endif /* Old version */
  4087.  
  4088. syms_of_fileio ()
  4089. {
  4090.   Qexpand_file_name = intern ("expand-file-name");
  4091.   Qdirectory_file_name = intern ("directory-file-name");
  4092.   Qfile_name_directory = intern ("file-name-directory");
  4093.   Qfile_name_nondirectory = intern ("file-name-nondirectory");
  4094.   Qunhandled_file_name_directory = intern ("unhandled-file-name-directory");
  4095.   Qfile_name_as_directory = intern ("file-name-as-directory");
  4096.   Qcopy_file = intern ("copy-file");
  4097.   Qmake_directory = intern ("make-directory");
  4098.   Qdelete_directory = intern ("delete-directory");
  4099.   Qdelete_file = intern ("delete-file");
  4100.   Qrename_file = intern ("rename-file");
  4101.   Qadd_name_to_file = intern ("add-name-to-file");
  4102.   Qmake_symbolic_link = intern ("make-symbolic-link");
  4103.   Qfile_exists_p = intern ("file-exists-p");
  4104.   Qfile_executable_p = intern ("file-executable-p");
  4105.   Qfile_readable_p = intern ("file-readable-p");
  4106.   Qfile_symlink_p = intern ("file-symlink-p");
  4107.   Qfile_writable_p = intern ("file-writable-p");
  4108.   Qfile_directory_p = intern ("file-directory-p");
  4109.   Qfile_accessible_directory_p = intern ("file-accessible-directory-p");
  4110.   Qfile_modes = intern ("file-modes");
  4111.   Qset_file_modes = intern ("set-file-modes");
  4112.   Qfile_newer_than_file_p = intern ("file-newer-than-file-p");
  4113.   Qinsert_file_contents = intern ("insert-file-contents");
  4114.   Qwrite_region = intern ("write-region");
  4115.   Qverify_visited_file_modtime = intern ("verify-visited-file-modtime");
  4116.   Qset_visited_file_modtime = intern ("set-visited-file-modtime");
  4117.  
  4118.   staticpro (&Qexpand_file_name);
  4119.   staticpro (&Qdirectory_file_name);
  4120.   staticpro (&Qfile_name_directory);
  4121.   staticpro (&Qfile_name_nondirectory);
  4122.   staticpro (&Qunhandled_file_name_directory);
  4123.   staticpro (&Qfile_name_as_directory);
  4124.   staticpro (&Qcopy_file);
  4125.   staticpro (&Qmake_directory);
  4126.   staticpro (&Qdelete_directory);
  4127.   staticpro (&Qdelete_file);
  4128.   staticpro (&Qrename_file);
  4129.   staticpro (&Qadd_name_to_file);
  4130.   staticpro (&Qmake_symbolic_link);
  4131.   staticpro (&Qfile_exists_p);
  4132.   staticpro (&Qfile_executable_p);
  4133.   staticpro (&Qfile_readable_p);
  4134.   staticpro (&Qfile_symlink_p);
  4135.   staticpro (&Qfile_writable_p);
  4136.   staticpro (&Qfile_directory_p);
  4137.   staticpro (&Qfile_accessible_directory_p);
  4138.   staticpro (&Qfile_modes);
  4139.   staticpro (&Qset_file_modes);
  4140.   staticpro (&Qfile_newer_than_file_p);
  4141.   staticpro (&Qinsert_file_contents);
  4142.   staticpro (&Qwrite_region);
  4143.   staticpro (&Qverify_visited_file_modtime);
  4144.  
  4145.   Qfile_name_history = intern ("file-name-history");
  4146.   Fset (Qfile_name_history, Qnil);
  4147.   staticpro (&Qfile_name_history);
  4148.  
  4149.   Qfile_error = intern ("file-error");
  4150.   staticpro (&Qfile_error);
  4151.   Qfile_already_exists = intern("file-already-exists");
  4152.   staticpro (&Qfile_already_exists);
  4153.  
  4154. #ifdef MSDOS
  4155.   Qfind_buffer_file_type = intern ("find-buffer-file-type");
  4156.   staticpro (&Qfind_buffer_file_type);
  4157. #endif
  4158.  
  4159.   Qcar_less_than_car = intern ("car-less-than-car");
  4160.   staticpro (&Qcar_less_than_car);
  4161.  
  4162.   Fput (Qfile_error, Qerror_conditions,
  4163.     Fcons (Qfile_error, Fcons (Qerror, Qnil)));
  4164.   Fput (Qfile_error, Qerror_message,
  4165.     build_string ("File error"));
  4166.  
  4167.   Fput (Qfile_already_exists, Qerror_conditions,
  4168.     Fcons (Qfile_already_exists,
  4169.            Fcons (Qfile_error, Fcons (Qerror, Qnil))));
  4170.   Fput (Qfile_already_exists, Qerror_message,
  4171.     build_string ("File already exists"));
  4172.  
  4173.   DEFVAR_BOOL ("insert-default-directory", &insert_default_directory,
  4174.     "*Non-nil means when reading a filename start with default dir in minibuffer.");
  4175.   insert_default_directory = 1;
  4176.  
  4177.   DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm,
  4178.     "*Non-nil means write new files with record format `stmlf'.\n\
  4179. nil means use format `var'.  This variable is meaningful only on VMS.");
  4180.   vms_stmlf_recfm = 0;
  4181.  
  4182.   DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist,
  4183.     "*Alist of elements (REGEXP . HANDLER) for file names handled specially.\n\
  4184. If a file name matches REGEXP, then all I/O on that file is done by calling\n\
  4185. HANDLER.\n\
  4186. \n\
  4187. The first argument given to HANDLER is the name of the I/O primitive\n\
  4188. to be handled; the remaining arguments are the arguments that were\n\
  4189. passed to that primitive.  For example, if you do\n\
  4190.     (file-exists-p FILENAME)\n\
  4191. and FILENAME is handled by HANDLER, then HANDLER is called like this:\n\
  4192.     (funcall HANDLER 'file-exists-p FILENAME)\n\
  4193. The function `find-file-name-handler' checks this list for a handler\n\
  4194. for its argument.");
  4195.   Vfile_name_handler_alist = Qnil;
  4196.  
  4197.   DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions,
  4198.     "A list of functions to be called at the end of `insert-file-contents'.\n\
  4199. Each is passed one argument, the number of bytes inserted.  It should return\n\
  4200. the new byte count, and leave point the same.  If `insert-file-contents' is\n\
  4201. intercepted by a handler from `file-name-handler-alist', that handler is\n\
  4202. responsible for calling the after-insert-file-functions if appropriate.");
  4203.   Vafter_insert_file_functions = Qnil;
  4204.  
  4205.   DEFVAR_LISP ("write-region-annotate-functions", &Vwrite_region_annotate_functions,
  4206.     "A list of functions to be called at the start of `write-region'.\n\
  4207. Each is passed two arguments, START and END as for `write-region'.  It should\n\
  4208. return a list of pairs (POSITION . STRING) of strings to be effectively\n\
  4209. inserted at the specified positions of the file being written (1 means to\n\
  4210. insert before the first byte written).  The POSITIONs must be sorted into\n\
  4211. increasing order.  If there are several functions in the list, the several\n\
  4212. lists are merged destructively.");
  4213.   Vwrite_region_annotate_functions = Qnil;
  4214.  
  4215.   DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers,
  4216.     "A list of file name handlers that temporarily should not be used.\n\
  4217. This applies only to the operation `inhibit-file-name-operation'.");
  4218.   Vinhibit_file_name_handlers = Qnil;
  4219.  
  4220.   DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation,
  4221.     "The operation for which `inhibit-file-name-handlers' is applicable.");
  4222.   Vinhibit_file_name_operation = Qnil;
  4223.  
  4224.   DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name,
  4225.     "File name in which we write a list of all auto save file names.");
  4226.   Vauto_save_list_file_name = Qnil;
  4227.  
  4228.   defsubr (&Sfind_file_name_handler);
  4229.   defsubr (&Sfile_name_directory);
  4230.   defsubr (&Sfile_name_nondirectory);
  4231.   defsubr (&Sunhandled_file_name_directory);
  4232.   defsubr (&Sfile_name_as_directory);
  4233.   defsubr (&Sdirectory_file_name);
  4234.   defsubr (&Smake_temp_name);
  4235.   defsubr (&Sexpand_file_name);
  4236.   defsubr (&Ssubstitute_in_file_name);
  4237.   defsubr (&Scopy_file);
  4238.   defsubr (&Smake_directory_internal);
  4239.   defsubr (&Sdelete_directory);
  4240.   defsubr (&Sdelete_file);
  4241.   defsubr (&Srename_file);
  4242.   defsubr (&Sadd_name_to_file);
  4243. #ifdef S_IFLNK
  4244.   defsubr (&Smake_symbolic_link);
  4245. #endif /* S_IFLNK */
  4246. #ifdef VMS
  4247.   defsubr (&Sdefine_logical_name);
  4248. #endif /* VMS */
  4249. #ifdef HPUX_NET
  4250.   defsubr (&Ssysnetunam);
  4251. #endif /* HPUX_NET */
  4252.   defsubr (&Sfile_name_absolute_p);
  4253.   defsubr (&Sfile_exists_p);
  4254.   defsubr (&Sfile_executable_p);
  4255.   defsubr (&Sfile_readable_p);
  4256.   defsubr (&Sfile_writable_p);
  4257.   defsubr (&Sfile_symlink_p);
  4258.   defsubr (&Sfile_directory_p);
  4259.   defsubr (&Sfile_accessible_directory_p);
  4260.   defsubr (&Sfile_modes);
  4261.   defsubr (&Sset_file_modes);
  4262.   defsubr (&Sset_default_file_modes);
  4263.   defsubr (&Sdefault_file_modes);
  4264.   defsubr (&Sfile_newer_than_file_p);
  4265.   defsubr (&Sinsert_file_contents);
  4266.   defsubr (&Swrite_region);
  4267.   defsubr (&Scar_less_than_car);
  4268.   defsubr (&Sverify_visited_file_modtime);
  4269.   defsubr (&Sclear_visited_file_modtime);
  4270.   defsubr (&Svisited_file_modtime);
  4271.   defsubr (&Sset_visited_file_modtime);
  4272.   defsubr (&Sdo_auto_save);
  4273.   defsubr (&Sset_buffer_auto_saved);
  4274.   defsubr (&Sclear_buffer_auto_save_failure);
  4275.   defsubr (&Srecent_auto_save_p);
  4276.  
  4277.   defsubr (&Sread_file_name_internal);
  4278.   defsubr (&Sread_file_name);
  4279.  
  4280. #ifdef unix
  4281.   defsubr (&Sunix_sync);
  4282. #endif
  4283. }
  4284.